2016-08-26 2 views
1

나는 주위를 둘러 보았고 사용할 수있는 솔루션을 혼자서 사용할 수 있도록하는 데 어려움을 겪었습니다. 내 사용자가 모든 행/열에서 용어를 검색 할 수 있도록 실시간으로 목록 상자를 필터링해야했습니다.실시간 텍스트 상자에서 액세스 용 필터 필터링

내가 가지고 있던 목록 상자가 바인딩되지 않았고 로직/코드를 사용하여 목록 상자의 데이터를 채울 다양한 쿼리를 호출하는 버튼을 사용했습니다. 모든 단추는 다른 수의 열을 가진 조회를 호출하므로 목록 상자는이 정보를 적절히 표시하도록 동적으로 변경됩니다.

나는이 문제에 관해 무엇인가를 찾기가 힘들었 기 때문에, 나는 내 개발 문제를 도와 준 훌륭한 사람들과 나의 해결책을 공유하기로 결정했다.

당신은 내가 포스트에 아무것도 해결 알려 주시면 더 나은 설명을 업데이 트됩니다 필요가 있다고 생각하는 경우

아래의 솔루션을 참조하십시오. 지적하는

답변

1

하나 개의 작은 것은, 내가 원래 형태에 전역 변수로 목록 상자에 저장 한 필터링되지 않은 레코드를 저장하는 것입니다. "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