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

天大地大妈咪最大 提交于 2019-12-24 15:06:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!