Referring to Controls

A control is any object that is placed on a form or report, like a label, text box, combo box, image, checkbox, and so onet cetera. To refer to a control (for example, a bound textbox named BusinessName) from the code behind a form or report, you use

Me!BusinessName

So, if you want to clear out the BusinessName control, you use

Me!BusinessName = Null

There has long been confusion in the VBA world about when to use a "!" (bang) and when to use a "." (dot). There are more technical ways to describe it, but for the average VBA programmer there's a quick rule that works most of the time: If you (or any programmer) named it, you can use a bang. If Access named it, you use a dot. Now, before all the VBA experts reading this get upset, please realize that this is a general guideline only, but it does help.

Now that we've just said that, here's an exception. In the last few versions of Access, you can use either a bang or a dot when referring to controls on forms or reports, even though you named them. This is because of a little trick Access does: it turns all of your controls into properties of the form or report, so they can be referred to with dots. This has a handy benefit: Access uses Intellisense to prompt you with the possible properties and methods that are available for an object. So, in the above example with Me!BusinessName, if you type Me and then <dot>, Access will prompt you with every method and property for the object Me, which includes your control BusinessName.

Note that the little trick about using a dot instead of a bang for controls on forms and reports does not extend to fields in a recordset. You still need to refer to those using bang, like this: recMyRecordset!BusinessName.

0 0

Post a comment