Set current record in a form on open

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form and I can retrieve the current record position like so:

Dim lRecord As Long
lRecord = Me.CurrentRecord

Now, how can I set the current record position to a specific record when a
form opens?

TIA
 
This should do it for you:
Open your form in design mode and open the form properties.
In the "On Open" property, specify "[Event Procedure]".
Use the following code in the resulting sub.

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord acDataForm, "YourFormName", acGoTo, TheRecordNumber
End Sub

Regards
 
In a multi user environment, this could easily return the wrong record if
another user has added or deleted a record. It also would fail if there is
any ordering or filtering in the record source or the table.
Never rely on record numbers in Access. They are only relative to the
current recordset. Even a requery can change the numbers if any of the above
events occur.

A better approach would be to use the primary key of the record and the
Where argument of the OpenForm method.

JackL said:
This should do it for you:
Open your form in design mode and open the form properties.
In the "On Open" property, specify "[Event Procedure]".
Use the following code in the resulting sub.

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord acDataForm, "YourFormName", acGoTo, TheRecordNumber
End Sub

Regards





XP said:
I have a form and I can retrieve the current record position like so:

Dim lRecord As Long
lRecord = Me.CurrentRecord

Now, how can I set the current record position to a specific record when a
form opens?

TIA
 
Klatuu, thanks for your input.

Klatuu said:
In a multi user environment, this could easily return the wrong record if
another user has added or deleted a record. It also would fail if there is
any ordering or filtering in the record source or the table.
Never rely on record numbers in Access. They are only relative to the
current recordset. Even a requery can change the numbers if any of the above
events occur.

A better approach would be to use the primary key of the record and the
Where argument of the OpenForm method.

JackL said:
This should do it for you:
Open your form in design mode and open the form properties.
In the "On Open" property, specify "[Event Procedure]".
Use the following code in the resulting sub.

Private Sub Form_Open(Cancel As Integer)
DoCmd.GoToRecord acDataForm, "YourFormName", acGoTo, TheRecordNumber
End Sub

Regards





XP said:
I have a form and I can retrieve the current record position like so:

Dim lRecord As Long
lRecord = Me.CurrentRecord

Now, how can I set the current record position to a specific record when a
form opens?

TIA
 
Back
Top