如何解决访问中的错误91

时间:2017-08-18 08:59:37

标签: vba ms-access access-vba

我在搜索按钮上运行此代码时遇到此错误。以下是我的代码。谢谢。

   Option Compare Database
   Option Explicit

    Private Sub txtsearch_Click()

        If IsNull(Searchbar) = False Then

             Me.Recordset.FindFirst "[PONumber]=" & Searchbar

             Me!Searchbar = Null

             If Me.Recordset.NoMatch Then

                   MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"

                    Me!Searchbar = Null

             End If

         End If

    End Sub

1 个答案:

答案 0 :(得分:1)

您必须保留记录集:

Private Sub txtsearch_Click()

    Dim rs As DAO.Recordset

    If IsNull(Me!Searchbar.Value) = False Then
         Set rs = Me.RecordsetClone
         rs.FindFirst "[PONumber]=" & Me!Searchbar.Value
         If rs.NoMatch Then
             MsgBox "No record found", vbOKOnly + vbInformation, "Sorry"
         Else
             Me.Bookmark = rs.Bookmark
         End If
         Me!Searchbar.Value = Null
     End If

     Set rs = Nothing

End Sub