View Full Version : how to filter the form with VBA without Enter Parameter Value popup


adsmmg
12-04-2007, 04:34 AM
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:
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:
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.

Jurjen
12-20-2007, 07:47 PM
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) & "'"

Pat Hartman
01-01-2008, 11:21 AM
Setting filters can be tricky. Try setting the filter in form view using the GUI. Then open the form in design view and see what Access placed in the filter. Sometimes you need the tablename as a qualifier.

Also, I don't see in your code where you turn the filter on. You set it but you also need to turn it on.