Qiita Conference 2025

ymrl (@ymrl)

がんばらないアクセシビリティ: より幅広く価値を届けるために

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-15

前回の内容)自分の端末にMicroSoft Accessがインストールされていることを知り、Access VBAに挑戦を決意。自分の分野であるHCL Notesの開発言語もBASIC言語であることから、Notesでモックアップを作成してNotesとAccess VBAを対比させて演習を進めることに。作成するのは社員情報データベース。今回から、Access側の開発に入ります。

今回、挑戦すること

  1. 生年月日フィールドに日付を入力すると、年齢を自動計算させる。(フィールドのイベントトリガーの確認)
  2. 生年月日フィールドに入力がない状態で保存するとき、メッセージを表示させる。(フォームのイベントトリガーの確認)
  3. 自動的に、今日の日付と生年月日を突き合せて年齢の更新を行う。(スケジュールエージェントの確認)

参考文献

MicroSoftのヘルプ情報

生年月日フィールドに日付を入力した後、年齢を自動計算させる。

フィールドのイベントトリガーは、NotesもAccessも大差はない。
Hikaku.png

Lotus Scriptの場合

Sub Exiting(Source As Field)
	
'現在の画面を取得
	Dim workspace As New NotesUIWorkspace
	Dim uidoc As NotesUIDocument
	Set uidoc = workspace.CurrentDocument
	
'誕生日と今日の日付を取得
	Dim birthday As New NotesDateTime(uidoc.FieldGetText("BirthDay"))
	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	
'年齢に計算結果をセット
	Call uidoc.FieldSetText ( "age", Cstr( age ) )	
End Sub

Access VBAの場合

Private Sub BirthDay_LostFocus()
    Dim y, m As Integer
    Dim age As Integer

    y = Year(Now) - Year(Me.BirthDay)
    m = Month(Now) - Month(Me.BirthDay)
    
    If m < 0 Or (m = 0 And Day(Me.BirthDay) < Day(Me.BirthDay)) Then
        age = y - 1
    Else
        age = y
    End If    
    Me.age = CStr(age)
End Sub

Lotus ScriptとAccess VBAを比較すると、自分自身を取得する方法が異なることが分かりました。それから、Lotus Scriptでは日付型にパラメータが必要ですが、Access VBAにはパラメータがありませんでした。
コードの構成はそのまま書けたので、お作法さえ押さえれば苦労はしないかも?

生年月日フィールドに入力がない状態で保存するとき、メッセージを表示させる。

フォームのイベントトリガーも、NotesもAccessも大差はありませんでした。
(フォームのイベントは、NotesもAccessも数が多すぎて1画面に収まらないため、画像は省略)

Lotus Scriptの場合

Sub Querysave(Source As Notesuidocument, Continue As Variant)
	If Source.FieldGetText("BirthDay") = "" Then
		Msgbox "生年月日の入力がありません", 16, "入力チェック"
		Continue =False
	End If
End Sub

Access VBAの場合

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If (IsNull(Me.BirthDay)) Or (Me.BirthDay.Value = "") Then
        MsgBox "生年月日の入力がありません", 16, "入力チェック"
        Cancel = True
    End If
End Sub

Acess VBAは、MicrosoftのBeforeUpdate イベントの解説を参考にしました。

ここでも、Meキーワードが使用されています。フロントエンドの処理はLotus Scriptよりも簡潔にコードが書けそうです。また、Me.BirthDay.Value の .value は省略しても問題なく動作しました。Lotus Scriptよりも細かい作法に気を使わなくても動作はするという印象を受けました。(もっとも、その分だけバグを見逃しがちになりそうですが…。)

※「自動的に、今日の日付と生年月日を突き合せて年齢の更新を行う。」は次の記事にまわします。

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
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?