하나 개의 작은 것은, 내가 원래 형태에 전역 변수로 목록 상자에 저장 한 필터링되지 않은 레코드를 저장하는 것입니다. "onChange"이벤트 트리거를 갖도록 텍스트 상자를 설정하고 아래 함수를 호출합니다. 목록 상자, 텍스트 상자의 사용자 문자열 및 필터되지 않은 레코드 세트에 대해 작성한 전역 변수를 전달합니다. 문자를 삭제할 때 원본 데이터를 다시 가져 오는 데 필요합니다.
또한이 함수는 숫자 열을 잘 처리하지 않습니다. 숫자 데이터 형식 열이있는 쿼리를 사용하여 일부 테스트를 수행 한 후에이 사실을 알게되었습니다. 이 문제를 해결하기 위해 CStr() 함수를 사용하여 숫자를 문자열로 반환하는 쿼리를 설정했습니다. 우리 초보자를 위해, 나는 단순히 이름 붙여진 쿼리에 들어가서 내 숫자 열을 찾고 Cstr을 "필드 행"에 넣었습니다. 예를 들어 "Customer Impacted"라는 숫자 열이 있습니다. 나는 쿼리에 들어간 후, [고객의 영향 칼럼]의 '필드 행'에 쓴 :
Customers_Affected : CSTR ([고객이 영향])
것은 당신이 경우 지연이 발생할 수 있음을주의 할 거대한 레코드 세트가 있습니다. 나는 단지 약 3000의 크기를 사용하고 있으며 아주 잘 실행됩니다. 즐겨.
Function realTimeFilter(ByVal List As Listbox, ByVal userString As String, ByVal staticRecordSet As DAO.Recordset)
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'\\\ \\\
'\\\ This Function allows the user to input any string to create a \\\
'\\\ Filter on a given listbox in realtime. \\\
'\\\ \\\
'\\\ The programmer gives this fucntion the listbox of values to filter, \\\
'\\\ The user's input string, and the unfiltered recordset that should be \\\
'\\\ held in a global variable on the form. \\\
'\\\ \\\
'\\\ I personally create a global called baseRecord. Everytime I update \\\
'\\\ the records in the listbox with a new unfiltered set, \\\
'\\\ I clone a copy to baseRecord. This allows \\\
'\\\ the user to delete strings from the filter and regain the old dataset \\\
'\\\ without having to query the data to the box again. \\\
'\\\ \\\
'\\\ enjoy! \\\
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
'declare variables
Dim rs As DAO.Recordset
Dim str() As String
Dim filterStr As String
Dim i As Integer
Dim k As Integer
'adds unfiltered recordset back to listbox and also puts the data into our set for manipulation
Set List.Recordset = staticRecordSet.OpenRecordset
Set rs = List.Recordset
'split the terms
str = Split(userString, ",")
'examine the textbox string after it has been parsed. Determine which set of logic to use:
'first set is for single search criteria. Second block is for multiple search criteria
If (UBound(str) = 0) Then
'loop through the column fields
For i = 0 To rs.Fields.Count - 1
'if not on last column add an "OR" to the end of the filter string. Else cap the string
If ((i < rs.Fields.Count - 2) Or (i = rs.Fields.Count - 2)) Then
filterStr = filterStr & " " & rs.Fields(i).Name & " like '" & Trim(str(0)) & "*' OR "
Else
filterStr = filterStr & " " & rs.Fields(i).Name & " like '" & Trim(str(0)) & "*'"
End If
Next i
'set the filter
rs.Filter = filterStr
Else
'start by enclosing the first logical string
filterStr = "("
'cycle through each word in the array of Strings
For i = LBound(str) To UBound(str)
'cycle through each column name in the recordset
For k = 0 To rs.Fields.Count - 1
'if not the final column add an "OR" at the end of the filter
If ((k < rs.Fields.Count - 2) Or (k = rs.Fields.Count - 2)) Then
filterStr = filterStr & " " & rs.Fields(k).Name & " like '" & Trim(str(i)) & "*' OR "
'if the final column AND string is not the last element add "AND (" to the end of the string to start the next
'portion of logic in the string
ElseIf ((i < UBound(str) And k = rs.Fields.Count - 1)) Then
filterStr = filterStr & " " & rs.Fields(k).Name & " like '" & Trim(str(i)) & "*') AND ("
'if last column and last string in the array, cap the filter string
Else
filterStr = filterStr & " " & rs.Fields(k).Name & " like '" & Trim(str(i)) & "*')"
End If
Next k
'add filter
rs.Filter = filterStr
Next i
End If
'set recordset and refresh the listbox
Set List.Recordset = rs.OpenRecordset
List.Requery
'housekeeping
rs.Close
Set rs = Nothing
End Function