Chapter 21: Building Multiuser Applications
783
For example, you shouldn’t create routines that require a recordset named Employees, because
the database may have unbound forms based on other tables that act the same way as the
Employees form. UnboundDisplay is a reusable function. Because you created the form as a
bound form first, the control names on the form should be the same as the field names in the
recordset. And, because you have the recordset open, the names of the controls on your form are
readily available. UnboundDisplay cycles through the recordset, setting the value of each form
control equal to the value of its corresponding recordset field value.
The following listing shows the code for UnboundDisplay:
Function UnboundDisplay( _
frm As Form, _
frmRS As DAO.Recordset) As Integer
Dim ctlName As String
Dim lngReturn As Long
Dim x As Integer
On Error GoTo HandleError
‘ Move to the first record in the table
frmRS.MoveFirst
‘ Cycle through the recordset,
‘ setting thevalue of each control
For x = 0 To frmRS.Fields.Count - 1
ctlName = frmRS.Fields(x).Name
frm.Controls(ctlName).Value = frmRS.Fields(x).Value
Next x
Display_End:
Exit Function
HandleError:
‘ If there’s an error, switch to the error
‘ handling procedure:
lngReturn = ErrorRoutine(0)
GoTo Display_End
End Function
Navigating through records
A user can move through records on the Employee form in either of two ways:
l (^) By using one of the navigation buttons I’ve created. (You can’t use Access’s navigation but-
tons on an unbound form.)
l (^) By searching for a record using a combo box.
Using navigation buttons
The navigation buttons I’ve created are based on four routines, each of which work similarly. The
UnboundMoveFirst and UnboundMoveLast routines accept the name of the form you’re
using and the open recordset as arguments. All they do is issue a MoveFirst or MoveLast
method on the recordset variable to move to the desired location.