问题
I have a Mainform
with textbox
and button
to search subform
record
it works fine when i directly open Mainform
and searching desire record
but when i open my form in Navigaition
form it gives me error.
Download My Access Project What i have tried.
Below is my code:
Private Sub cmdSearch_Click()
Dim MainFK As Long
MainFK = DLookup("MainformID", "Subform", "SubformID =" & Me.txtSearch)
Debug.Print MainFK
DoCmd.SearchForRecord acDataForm, "Mainform", acFirst, "MainformID=" &MainFK
End Sub
See Screen Shot:
回答1:
I think DoCmd.SearchForRecord
is tricky on subforms. Try this instead:
Private Sub cmdSearch_Click()
Dim MainFK As Long
Dim rs As DAO.Recordset
Dim WhereStr As String
MainFK = DLookup("MainformID", "Subform", "SubformID =" & Me.txtSearch)
WhereStr = "MainformID=" & MainFK
With Me.Form
Set rs = .RecordsetClone
rs.FindFirst WhereStr
If _
rs.NoMatch _
Then
MsgBox "Subform record not match to mainform record"
Else
.Bookmark = rs.Bookmark
End If
End With
End Sub
Here's your file back: https://drive.google.com/file/d/0B-J5B7nFljZiLVJ1dEtoTVQwcXc/view?usp=sharing
来源:https://stackoverflow.com/questions/30001302/referancing-form-in-docmd-searchforrecord-when-using-navigation-form-access