0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

NotesデータベースをMicroSoft Accessで作ってみる(誕生日の検出)

Last updated at Posted at 2021-01-18

HCL Notesの「自動的に、今日の日付と生年月日を突き合せて年齢の更新を行う」エージェントをAceess VBAでの再現に挑戦……と思ったのですが、Accessの作法に手間取って完成には至りませんでした……。

Notes側のコード

Note側はデータベースから誕生日が一致する社員情報フォームを抽出して、更新をするエージェントを書くだけです。

Sub Initialize
	Dim ss As New NotesSession
	Dim db As NotesDatabase
	Set db = ss.Currentdatabase
	
'実行日が誕生日と一致する社員情報をCollection	
	Dim Query As String
	Query = "Form = ""Main"" &"
	Query = Query & "@Text(@Month(Birthday))+@Text(@Day(Birthday))="
	Query = Query & "@Text(@Month(@Today))+@Text(@Day(@Today))"
	
	Dim col As NotesDocumentCollection
	Dim doc As NotesDocument
	
	Set col = db.Search(Query, Nothing, 0)

'文書がなければ終了	
	If col.Count = 0 Then 
		Exit Sub
	End If
	
'一致する文書に再計算を実行	
	Set doc = col.GetFirstDocument
	Do Until ( doc Is Nothing )
		Call AgeUpdate(doc)
		Call doc.Save( False, False )
		Set doc = col.GetNextDocument( doc )
	Loop
End Sub

REM
	Sub AgeUpdate
	Description: "前回作成したコードを使いまわし"
END REM
Sub AgeUpdate(doc As NotesDocument)	
'誕生日と今日の日付を取得
	Dim birthday As New NotesDateTime(doc.BirthDay(0))
	Dim nowday As New NotesDateTime( "Today" )	
	
'今日と誕生日の差分を計算
	Dim y ,m As Integer	
	Dim age As Integer
	y = Year(nowday.LSLocalTime) - Year(birthday.LSLocalTime)
	m = Month(nowday.LSLocalTime) - Month(birthday.LSLocalTime)
	
	If m < 0 Or (m = 0 And Day(nowday.LSLocalTime) < Day(birthday.LSLocalTime)) Then
		age = y - 1
	Else
		age = y
	End If
	
	'年齢に計算結果をセット
	doc.age = CStr( age )
End Sub

Access側のコード

年齢計算のコードはMicroSoftのドキュメントにあったので、これを流用しようと思ったのですが…。色々な作法を知らないといけないようで。

Accessの作法

コードを書きながらなので、箇条書きで。

  • Access VBAを読み込ませるときは、[作成] - [マクロ] - [プロシージャの実行]でプロシージャ(VBA)を指定する。指定できるのはFunctionのみでSubは利用できない。なお、Functionは引数なしでも問題ない。
  • Notesの Dim db As New NotesDatabase は、Dim db As DAO.Database という記述になる。オブジェクトはDAO(Aceess)/ADO(外部データベース)の2種類がある。
  • テーブル操作は .OpenRecordset メソッドで行う。
  • 「ビューを開く」は CurrentDB.OpenRecordset("テーブル名")
  • 「コレクションを集める」は CurrentDB.OpenRecordset("SELECT * FROM テーブル名")
  • Notesのデバッグモードと類似した画面は、VBAでは[表示]-[ローカルモード]で表示させることができる。

Notesのデバッグモード
notes.png
VBAのローカルモード
VBA.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?