how to filter the form with VBA without Enter Parameter Value popup

adsmmg

Registered User.
Local time
Today, 03:07
Joined
Nov 27, 2007
Messages
10
i have a search form that user can access to a sublist of records by choosing the parameter in combobox. this wirk good for one of comboboxs but display the popup form to enter the parameter value and after inserting the criteria it doesnt work. code for find button:
Code:
Private Sub cmdFind_Click()
Dim strfilter As String
On Error GoTo Err_
Me.sbfrmSearch.Form.FilterOn = False
Select Case searchItem
Case 1
    strfilter = "[Old Tel  No#]='" & CStr(Me.txtOldNumber.Value) & "'"
    lblCount.Caption = strfilter & "   ______case 1-----" & CStr(searchItem)
    Me.sbfrmSearch.Form.Filter = strfilter
    
Case 2
    strfilter = "[New Tel  No#]='" & Me.txtNewNumber.Value & "'"
    lblCount.Caption = strfilter & "   _______case 2-----" & CStr(searchItem)
    Me.sbfrmSearch.Form.Filter = strfilter
    
Case 3
    strfilter = "[Last Name]='" & Me.txtLastName.Value & "'"
    lblCount.Caption = strfilter & "  _______case 3-----" & CStr(searchItem)
    Me.sbfrmSearch.Form.Filter = strfilter
    
Case 4
    strfilter = "[Station Code]='" & CStr(Me.cboJobtitle.Value) & "'"
    lblCount.Caption = strfilter & "   ________case 4-----" & CStr(searchItem)
    Me.sbfrmSearch.Form.Filter = strfilter
    lblCount.Caption = "case 4"
Case 5
    strfilter = "[Job titles]='" & CStr(Me.cboSite.Value) & "'"
    lblCount.Caption = strfilter & "   _______case 5-----" & CStr(searchItem)
    Me.sbfrmSearch.Form.Filter = strfilter
    
End Select
    Me.sbfrmSearch.Form.FilterOn = True
    lbltest.Caption = Me.sbfrmSearch.Form.Recordset.RecordCount
   
Sub_Resume:
    Exit Sub
Err_:
MsgBox "Error Number: " & Err.Number & "   " & Err.Description

End Sub

and code for sensing the search criteria:
Code:
Private Sub txtNewNumber_BeforeUpdate(Cancel As Integer)
txtOldNumber = Null
txtLastName = Null
cboJobtitle = Null
cboSite = Null
searchItem = 2
End Sub
what is wrong in using the "filter". another thing is that the count of filtered record is displaying after two click on find button.
 
You are prompted for a parameter value, because you explicitly create one, namely [Old Tel No#] in this example.

strfilter = "[Old Tel No#]='" & CStr(Me.txtOldNumber.Value) & "'"
lblCount.Caption = strfilter & " ______case 1-----" & CStr(searchItem)
Me.sbfrmSearch.Form.Filter = strfilter

Obviously, that is not what you want. Furthermore, in this way you cannot get your filter to work, because it will just compare two textstrings, evaluating to either True or False for all your records.
Your filter is applied to the recordsource of the form, so what you should do is determine the name of the relevant table field and add that to your expression:

strfilter = "<field_name> ='" & CStr(Me.txtOldNumber.Value) & "'"
 

Users who are viewing this thread

Back
Top Bottom