Sunday, May 31, 2009

Entering simple VBA code in an Access form

To enter code for an access object, open the form (or other entity that needs code) in Design view.
Right click on the object (in this example, a button) that will require the code. Select the event tab and the property where the code will be used, such as "On click" if you want to make a button do something.

Once you have opened code builder for the desired property, you may enter code to make it all work.

The code example below is actually not meant for a button, but is one of the handiest lines of code I have ever used. It is perfect to add a date /time stamp in the 'txtupdated' (calculated) field. If me.dirty refers to if the object, such as record within a form, and specifies if it has been changed, then txtupdated = now() adds today's date/time to the txtupdated field. Now this useful code is great if you ever need to look at new entries or edits from a certain date range... or just as a nice way to track when data entry has been done. Especially useful if there is more than 1 person using a database.