Open and position on a specific record

  • Thread starter Thread starter XMan
  • Start date Start date
X

XMan

How can I open a form and position on a specific record and be able to do previous and next command? I can open with link criteria to a specific record and that is it. I can't see previous and next record. TIA.
 
XMan said:
How can I open a form and position on a specific record and be able
to do previous and next command? I can open with link criteria to a
specific record and that is it. I can't see previous and next record.

There are two main approaches: (1) let code behind the form do most of
the work, or (2) do the work at the point where you open the form. Here
are very simplified forms of both:

(1) The form does the work.

Where you open the form:

Dim lngRecordID As Long

' ... set lngRecordID to the key you want to find ...

DoCmd.OpenForm "MyForm", OpenArgs:=lngRecordID

In the form's Load event:

Private Sub Form_Load()

Dim strRecordToFind As String

strRecordToFind = Me.OpenArgs & ""

If Len(strRecordToFind) <> 0 Then

With Me.RecordsetClone
.FindFirst "ID=" & strRecordToFind
If Not .NoMatch Then
Me.Bookmark = .Bookmark
End If
End With

End If

End Sub

(2) Caller does the work (no special code in form)


Dim lngRecordID As Long
Dim frm As Form

' ... set lngRecordID to the key you want to find ...

DoCmd.OpenForm "MyForm"

Set frm = Forms("MyForm")
With frm.RecordsetClone
.FindFirst "ID=" & strRecordToFind
If Not .NoMatch Then
frm.Bookmark = .Bookmark
End If
End With
Set frm = Nothing
 
Dirk, It's working great! THANKS!!!!


Dirk Goldgar said:
There are two main approaches: (1) let code behind the form do most of
the work, or (2) do the work at the point where you open the form. Here
are very simplified forms of both:

(1) The form does the work.

Where you open the form:

Dim lngRecordID As Long

' ... set lngRecordID to the key you want to find ...

DoCmd.OpenForm "MyForm", OpenArgs:=lngRecordID

In the form's Load event:

Private Sub Form_Load()

Dim strRecordToFind As String

strRecordToFind = Me.OpenArgs & ""

If Len(strRecordToFind) <> 0 Then

With Me.RecordsetClone
.FindFirst "ID=" & strRecordToFind
If Not .NoMatch Then
Me.Bookmark = .Bookmark
End If
End With

End If

End Sub

(2) Caller does the work (no special code in form)


Dim lngRecordID As Long
Dim frm As Form

' ... set lngRecordID to the key you want to find ...

DoCmd.OpenForm "MyForm"

Set frm = Forms("MyForm")
With frm.RecordsetClone
.FindFirst "ID=" & strRecordToFind
If Not .NoMatch Then
frm.Bookmark = .Bookmark
End If
End With
Set frm = Nothing

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Back
Top