Microsoft Access 2010 Bible

(Rick Simeone) #1

Part III: More-Advanced Access Techniques


782


Opening the form
The first event you must respond to is the Open event of the form. When the Open event fires,
two things need to be done to populate the unbound form:

l (^) Open the recordset filled with data displayed on the form. You don’t have to do this —
you could just open a new recordset instance in each routine behind the form — but it’s
faster to just have a recordset open and waiting as the form opens.
l Populate the form with data. In fact, the form is populated with data from the very first
record in the recordset. Again, you don’t have to, but many times a user expects to see
something on a form when it opens.
The Declarations section of the form contains two public variables, db (database) and rs (record-
set). The form’s Open event runs a very short procedure that sets the database and recordset vari-
ables to their proper values and calls the UnboundDisplay routine that loads the first record of
the recordset into the form’s fields:
Public db As DAO.Database
Public rs As DAO.Recordset
Private Sub Form_Open(Cancel As Integer)
Dim iReturn As Integer
Set db = CurrentDb
Set rs = db.OpenRecordset(“Employees”)
iReturn = UnboundDisplay(Me, rs)
End Sub
The real work begins in the UnboundDisplay routine. Like most of the unbound methods, this
routine accepts the name of the form and the name of the open recordset as parameters. The key to
making this practical is making your routines as reusable as possible — this is good practice any-
time but especially in situations where you’ll be doing the same type of action more than once.
You tell the form what recordset you want to use by placing its name in the form’s Tag property. The
Tag property is unique in that it doesn’t do anything. It’s just a handy place to put some text that you
intend to use in your VBA code. You can use the Tag property as a place to store information for later
retrieval or to keep track of where you are in a process.
Here’s a good example of what you can do with the Tag property: Suppose you want your application
to have the ability to fill in each new record with data from the previous record. Using the Tag prop-
erty, you can store the current record’s values in each control’s Tag property, and, upon moving to the
next new record, fill in the form’s controls with the values stored in the Tag. It’s a whole lot easier than
doing a lookup using a hidden field on a form and the Seek method on a clone of the form’s recordset,
which is an alternative I’ve seen some people use.
The Tag property was introduced way back in Access Version 2, but many developers don’t know it
exists or aren’t sure what it’s for. I encourage you to use this property often.
The Tag property

Free download pdf