You Talking to Me

Programmers are typically self-obsessed beings, so on your journey to learn about Form Automation, it is important to understand that "It's all about Me!" No, Me is not about You, Me is about the currently active Form. If you want to know anything about the Form, you can request information from the Me object. Using Me will expose all of the events, properties, and controls associated to it. To utilize Me in your code, enter Me . (dot or period) and then select the desired value from the IntelliSense combo box, which automatically appears. See Figure 10-3.

Private Function

Get Value (pstrCcntrclTjaifie As String) As String

GetValue = He.

Centre.Is rpstrContrclT'iame) .Value








: On Activate

: OnApplyFilter



■ OnClick ' OnClose





OnConnect OnOurrent


Figure 10-3

An example of using Me is if you want to know whether the user edited anything about the current record, you can inquire with the following:

If Me.Dirty then

Or, if you would like to change the caption of the current form, you could use the following command to change the Caption property:

Me.Caption = "Employees Form - Edited"

Finally, there may be times when you need to examine a value about a form but have no knowledge ahead of time what the "about" may be. There are many collections contained within the Me object that you can expose, even with very little information about the collection contents.

For example, suppose you need to reference the value of a certain control, but you don't know the name of the control before it happens. The ability to refer to that control in code using the specific name (that is, txtFirstName) will not work, so you may need a generic procedure, and need the flexibility to send it a variety of information like the following:

Private Sub DoSomething(pstrControlName

As String)

If Me.Controls(pstrControlName).Value

>50 then

...'Do something


...'Do something else

End If

End Function

This type of programming begins to scare the faint of heart, but it demonstrates not only a practical use of the Me object, but also the power and flexibility that VBA possesses. In the above example, the Me object contains a collection called Controls, which is the same as referencing a control name directly. To this collection, a parameter can be passed in the shape of either a literal value (that is, "txtFirstName", quotes required), a variable that contains the value (that is, pstrControlName), or an ordinal value (that is, zero (0) represents the first value in the collection).

The Me object is a very important and useful tool that can be used in any of the many event procedures that are written.

0 0

Post a comment