Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Filter VBA

Tags:

vba

ms-access

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?

like image 411
user569709 Avatar asked Jan 10 '11 10:01

user569709


People also ask

How do you filter data in Access?

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.

How do I filter an Access query?

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.

Can you use VBA in Access?

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.


1 Answers

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.

like image 148
HansUp Avatar answered Oct 30 '22 03:10

HansUp