使用导航表单时在 DoCmd.SearchForRecord 中引用表单 - Access

Referancing form in DoCmd.SearchForRecord when using Navigation form - Access

我有一个 Mainformtextboxbutton 来搜索 subform 记录 当我直接打开 Mainform 并搜索愿望记录时,它工作正常 但是当我以 Navigaition 形式打开我的表格时,它给了我错误。

Download My Access Project 我试过的。

下面是我的代码:

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

查看屏幕截图:

我认为 DoCmd.SearchForRecord 在子表单上很棘手。试试这个:

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

这是你的文件:https://drive.google.com/file/d/0B-J5B7nFljZiLVJ1dEtoTVQwcXc/view?usp=sharing