Part III: More-Advanced Access Techniques
784
Function UnboundMoveFirst( -
frm As Form, _
frmRS As DAO.Recordset) As Integer
Dim ctlName As String
Dim x As Integer
Dim lngReturn As Long
On Error GoTo HandleError
frmRS.MoveFirst
For x = 0 To frmRS.Fields.Count - 1
ctlName = frmRS.Fields(x).Name
frm.Controls(ctlName).Value = frmRS.Fields(x).Value
Next x
ExitHere:
Exit Function
HandleError:
‘Call ErrorRoutine, specifying zero retries:
lngReturn = ErrorRoutine(0)
GoTo ExitHere
End Function
Note
The code for UnboundMoveLast is not given here because it’s nearly identical to UnboundMoveFirst.
Simply use MoveLast instead of MoveFirst, and you’ve got UnboundMoveLast.
The UnboundMoveNext and UnboundMovePrevious procedures accept the name of the form
and recordset, but they also include the employee ID of the currently displayed employee record.
When you call the MoveNext or MovePrevious procedure, the function that was called sets the
index of the recordset to the primary key and does a seek to place the cursor at the name of the
employee whose record is displayed. If the record is found (If Not frmRS.NoMatch), you issue
a MoveNext or MovePrevious method on the recordset to move to the desired record. The last
step in the procedure is to update the controls on your form with data from the recordset.
Notice that the code in UnboundMoveNext fails if the MoveNext moves the record pointer off
the end of the recordset. This issue will be trapped by the error handler, and UnboundMoveNext
calls the ErrorRoutine procedure, passing 0 as its argument. The user sees a message (displayed
from ErrorRoutine) that the operation failed.
Note
The code for UnboundMovePrevious is not given here because it is nearly identical to UnboundMoveNext.
Simply use MovePrevious instead of MoveNext, and make a few other minor changes and you’ve got
UnboundMovePrevious.
Function UnboundMoveNext( _
frm As Form, _
frmRS As DAO.Recordset,
lValue As Long) As Integer
Dim ctlName As String
Dim x As Integer