14 min read
Responding to Events with VBA
Choose [Event Procedure] in a control's Property Sheet to open the Visual Basic Editor with an empty, correctly named procedure already waiting for your code.
Field Notes: Code Attached to a Click
The most common everyday use of VBA in Access is attaching code directly to a specific control's event, similar in spirit to attaching a macro, but with the full flexibility of real code. Select a control — like a button — on a form in Design View, open its Property Sheet, click the Event tab, find the relevant event, such as On Click, click its dropdown, and choose "[Event Procedure]." Click the small "..." (Build) button that appears beside it, which opens the Visual Basic Editor with an empty procedure already created and correctly named for that specific event. Type your VBA code between the automatically generated Private Sub and End Sub lines.
For example, code inside a button's Click event might validate that a required field isn't empty before allowing a record to be saved, showing a custom, friendly message box if it is — something a plain macro could also do, but which becomes far more flexible in VBA as the logic grows more specific.
More lessons in Microsoft Access · Next: Importing Data from Excel and Text Files · Previous: Writing a Simple VBA Procedure
