office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

access使用書籤的方法篩選記録

2020-02-18 08:00:00
tmtony8
原創
4515

查找記録的控件顯示在窗體時,FindRecord方法是一種非常好的搜索方式。

如果要搜索是單箇值,可以用《access組閤框篩選綁定窗體的記録》方法

但是很多情況下,多箇值作爲查找條件會使用書籤的方法查找記録,如下設計視圖


詳細源代碼:

Private Sub cboQuickSearch_AfterUpdate()
  
  Dim rsClone As DAO.Recordset
  Dim sCriteria As String
  
  Const sSEARCHFLD As String = "[ProductID]"
  
  If Not IsNull(Me.cboQuickSearch.Value) Then

    Set rsClone = Me.RecordsetClone
    
    ' Build the criteria:
    sCriteria = sSEARCHFLD & " = " & Me.cboQuickSearch.Value
    
    ' Perform the search:
    rsClone.FindFirst sCriteria
    
    If Not rsClone.NoMatch Then
        'Synchronize the form's bookmark
        'to the recordset's record:
        Me.Bookmark = rsClone.Bookmark
    End If
    
    rsClone.Close
    Set rsClone = Nothing
    
  End If
  
End Sub

篩選效果如圖所示


使用findFirst或者bookmark方法要好於使用findrecord,因爲牠允許使用更複雜的條件,併且不需要設置被搜索的控件爲可見。也不需要將光標定位到某箇控件上卽可使用。

説明一下,RecordsetClone屬性創建的記録集是DAO類型的記録集。隻有DAO記録集支持Findfirst、findLast、Findnext、findprevious 方法。

    分享