Access VBA Macro Programming

(Joao Candeias) #1

Version


Versionwill tell you the version of Access that is being run. This is useful to know when the
user loads your application because the application may load correctly and begin running, but
you may have made use of features peculiar to a later version of Access. You can then display
a warning message or even force the user to quit the application.


If Application.Version <> "12.0" Then
MsgBox "You need a later version of Access to run this
application", vbCritical
Quit
End If


Me Object


TheMeobject is a special object that applies to forms and reports. Since it represents the
active form or report, it can only be used on modules for forms or reports and not on inserted
modules.
You can also use the forms or reports collection instead:


Forms("MyForm")


However, when writing code on a form or report module, it is easier to use theMeobject.

Main Properties, Methods, and Collections


This section details the main properties, methods, and collections you will use within theMe
object.


ActiveControl


ActiveControlrepresents the active control on the form or report. You cannot reference it on
the Form Load or Form Activate events, because at that point there is not an active control.


Private Sub Detail_Click()
MsgBox Me.ActiveControl.Name


End Sub


This will return the name of the control that has the focus.
If the control is a list box or drop-down, you can use the object to get the item selected.
This code tests whether the first item in the list has been selected. It returns True (-1) if it has
been selected, and False(0) if it is not selected.


MsgBox Me.ActiveControl.Selected( 0 )


Chapter 15: The Main Objects 201

Free download pdf