Setting filter with variables

OBBurton

Registered User.
Local time
Today, 04:26
Joined
Dec 26, 2013
Messages
77
I am trying to set a filter on a form with VBA using variables and having no success at all. The code I tried last, which seemed to be "close, but no cigar", was:
Code:
Dim sFilterValue As String
    Me.FilterOn = False
    sBiller = Me.txtbiller.Value
    sLastSource = Me.txtbiller.ControlSource
    sLastSource = "[" & sLastSource & "]"
    sFilterValue = sLastSource & "=" & sBiller
    Me.Filter = sFilterValue
    Me.FilterOn = True
sLastSource and sBiller are global variables. When I debug this code the sFilterValue is exactly what I would plug in manually and the Me.Filter shows up as "[Field Name] = Filter Value". But it crashes on Me.Filter = sFilterValue. I have tried all sorts of combinations but nothing I've tried works. What is the proper syntax for using variables as filters?
 
Last edited:
if your field is a text field, you will need to enclose your value in single quotes like this:

Code:
Dim sFilterValue As String
    Me.FilterOn = False
    sBiller = Me.txtbiller.Value
    sLastSource = Me.txtbiller.ControlSource
    sLastSource = "[" & sLastSource & "]"
    sFilterValue = sLastSource & "='" & sBiller & "'"
    Me.Filter = sFilterValue
    Me.FilterOn = True
 
Thanks!
It took me a minute to figure out the syntax but thanks to your example, I think I have it.
 

Users who are viewing this thread

Back
Top Bottom