I'm trying to use a filter in vba like this:
Private Sub Form_Load()
Me.Filter = "[Alvo] = " & AlvoAtual
Me.FilterOn = True
Me.Requery
End Sub
Where AlvoAtual is global variable, but nothin happens. When I change the AlvoAtual for a specifc value nothin happens too. Like this:
Private Sub Form_Load()
Me.Filter = "[Alvo] = 'AAAA'"
Me.FilterOn = True
Me.Requery
End Sub
Someone knows the problem?
I am adopting this question (VBAWhatnow) in the hope of it getting answered rather than make a duplicate as I was advised.
I am trying to do the same thing except with local variables.
My filter works correctly when I manually define the values but when I introduce the variables the filter no longer works
Forms![frmPendingActions]![qryPendingAction subform].Form.Filter = "Filterby = FilterCrit"
Could anyone help me find a good solution?
On the Home tab, in the Sort & Filter group, click Advanced and then click Advanced Filter/Sort on the shortcut menu. Add the fields on which you want to filter to the grid. In the Criteria row of each field, specify a criterion.
To filter data in a query, open it in Datasheet View, click the down-arrow at the top of a column, and select a filter option. Here's how it looks in an Access app: You can select multiple values from the list, but in an app, the filter list closes each time you select an option.
Like macros, VBA lets you add automation and other functionality to your Access application. You can extend VBA by using third-party controls, and you can write your own functions and procedures for your own specific needs.
You (VBAWhatnow) said "My filter works correctly when I manually define the values but when I introduce the variables the filter no longer works".
Then in your filter assignment ...
.Filter = "Filterby = FilterCrit"
So I'm assuming FilterCrit
is the name of your local variable. If that is true, build the filter expression using the variable's value rather than its name.
If Filterby
is a numeric field type ...
.Filter = "Filterby = " & FilterCrit
If Filterby
is a text field type ...
.Filter = "Filterby = """ & FilterCrit & """"
If neither of those variations is the answer for you, give us more information about Filterby
and FilterCrit
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With