2
1

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.

【Access】コントロールオブジェクトに対応するイベント一覧

Posted at

概要

Accessフォームで使用可能なコントロール (CommandButton、Labelなど) で、
それぞれがどのイベントに対応しているかをまとめた。

コントロールのイベント一覧

コントロールオブジェクト オブジェクトタイプ定数 番号 AfterUpdate AttachmentCurrent BeforeNavigate2 BeforeUpdate Change Click DblClick Dirty DocumentComplete Enter Exit GotFocus KeyDown KeyPress KeyUp LostFocus MouseDown MouseMove MouseUp NavigateError ProgressChange Updated NotInList Undo
Attachment acAttachment 126 - - - - - - -
BoundObjectFrame acBoundObjectFrame 108 - - - - - - - - -
CheckBox acCheckBox 106 - - - - - - - - - -
ComboBox acComboBox 111 - - - - - -
CommandButton acCommandButton 104 - - - - - - - - - - - -
ActiveX acCustomControl 119 - - - - - - - - - - - - - - - - - - -
EmptyCell acEmptyCell 127 - - - - - - - - - - - - - - - - - - - - - - - -
Image acImage 103 - - - - - - - - - - - - - - - - - - -
Label acLabel 100 - - - - - - - - - - - - - - - - - - -
Line acLine 102 - - - - - - - - - - - - - - - - - - - - - - - -
ListBox acListBox 110 - - - - - - - - - -
NavigationButton acNavigationButton 130 - - - - - - - - - - - -
NavigationControl acNavigationControl 129 - - - - - - -
Unbound ObjectFrame acObjectFrame 114 - - - - - - - - - - - - - -
OptionButton acOptionButton 105 - - - - - - - - - -
OptionGroup acOptionGroup 107 - - - - - - - - - - - - - - -
Page acPage 124 - - - - - - - - - - - - - - - - - - -
PageBreak acPageBreak 118 - - - - - - - - - - - - - - - - - - - - - - - -
Rectangle acRectangle 101 - - - - - - - - - - - - - - - - - - -
SubForm acSubForm 112 - - - - - - - - - - - - - - - - - - - - - -
Tab acTabCtl 123 - - - - - - - - - - - - - - -
TextBox acTextBox 109 - - - - - - -
ToggleButton acToggleButton 122 - - - - - - - - - -
WebBrowserControl acWebBrowser 128 - - -

何故か

Accessはフォームを扱う性質上コントロールが多くなりがち。
個別にイベント定義を書いていくと散らかりがち。
プロパティウィンドウが散らかりがち。

image.png

という理由から、イベントを1箇所にまとめて扱うというのはよくやる手法。
まとめ方は色々あると思うが、個人的にはフォームのロード時に
使用するしないは関係なくすべてのコントロールにイベントの着火先を定義する方法を好んで使う。
例えばすべてのボタンはクリックされると関数 OnClickEvent を自身のコントロール名付きで呼び出し、
OnClickEvent はコントロール名に応じて対応する処理を行う。
コードの視認性がよく気に入っている。

Private Sub Form_Load()
    On Error Resume Next
    Dim con As Control
    For Each con In Me.Controls
        con.OnClick = "=OnClickEvent(""" & con.Name & """)"
        con.OnChange = "=OnChangeEvent(""" & con.Name & """)"
        
        '~以下省略~
    
    Next
    On Error GoTo 0
End Sub

Private Function OnClickEvent(ByVal ctrName As String)
    Select Case ctrName
        Case "Btn_ファイル読み込み"
            '処理
            
        Case "Btn_メール送信"
            '処理
        
        Case "Btn_閉じる"
            DoCmd.Close acForm, Me.Name

        ' などなど

    End Select
End Function

しかし手抜きというか、Form_Load 内で On Error Resume Next を使用していた。
(LabelにAfterUpdateなど、エラーを無視してすべてのコントロールにイベントをブチ込んでいくイメージ)

美しくないので、ちゃんとコントロールタイプごとに振り分けようと思ったのだが、
Microsoft公式リファレンスでは
「イベント => それに対応したコントロール」 を一覧するのに苦労したので。

参考文献

Microsoft Access 公式リファレンス
https://docs.microsoft.com/ja-jp/office/vba/api/overview/access/object-model

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?