Macros and VBA
Harry
· 12 Sep 2026
· 14 views
Macros - No-Code Automation
Create → Macro opens the designer. Actions like OpenForm, RunSQL, MessageBox and SetValue chain with conditions:
If [Balance] > 1000
MessageBox: "High value customer"
Else
GoToRecord: New
End IfAttach macros to command buttons, forms' OnOpen or the database's startup so manual steps happen automatically.
VBA - Real Code
Create → Macro → Visual Basic (Alt+F11) for the VBA editor. Click an event property (e.g. button’s OnClick) and choose [Event Procedure]:
Private Sub cmdSave_Click()
If IsNull(Me.[LastName]) Then
MsgBox "Last name is required" , vbExclamation
Me.[LastName].SetFocus
Exit Sub
End If
DoCmd.RunCommand acCmdSaveRecord
End SubThe Object Model (Top 3)
- DoCmd - run commands: OpenForm, OpenReport, RunSQL.
- Forms / Reports - the open UI:
Forms!frmCustomers,Meis the current form. - CurrentDb - the database:
CurrentDb.Execute "...SQL...".
Error Handling
On Error GoTo ErrHandler
... your code ...
ErrHandler:
MsgBox "Error " & Err.Number & ": " & Err.DescriptionSecurity and Trust
Access shows a security warning for databases with code. Set Trust Center → Macro Settings → Disable all macros with notification and only enable databases you trust. Signed databases and VBA passwords add extra protection.
Key Points
- Start with macros; graduate to VBA when logic grows.
- Control everything from the form’s event properties.
- Handle errors and keep macro security at notification level.