By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

How to apply filter in a query?

100+
P: 180
I have a query name ICISquery, table field names are ARE of Employee, Date of ARE, Item Description and Remarks.
The table field ARE of Employee is a name of employee.
ICISquery has a 10000 records. I have a form name (home) in this form has a combolist and its row source are the name of employees. I have another form name (list) in datasheet view. when i click one of the employee in combolist, the list form will show up and its recordsource is ICISquery. But i want ICISquery will show only the records of employee i have clicked. For example, i click Juan Reynulfo, the list form will show up and contains only the records of Juan Reynulfo. Is this the work of filter in a query? If so, how can i apply this filter in query when you call it?
Dec 27 '11 #1

✓ answered by Mihail

Hi again, eneyardi.
Fortunately for you I know what you wish from this thread:
How to shorten codes that using if, else and end if?
Because from actual thread I can't understand :).

So, one way to do this is to make a new module (or use an existing one.
In this module delare a PUBLIC variable. Say strName and create a PUBLIC function (say fGetName)
Something like this:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public strName As String
  4.  
  5. Public Function fGetName() As String
  6.     fGetName = strName
  7. End Function

In your query, in the field ARE criteria row, write: fGetName().

Close the query and the module (of course save the changes).

Now, go to your combo box (in design view) and, under On Click event write this code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ComboName_Click()
  2.     strName = ComboName
  3. End Sub

That must be all to do.
From now to ever, when you select a new name from your combobox this name will be stored in strName variable. So, when you run the query (or something else like a form or a report based on this query) the query itself will apply (from the criteria row) the function fGetName which will return the value stored in strName (the name you preview selected in your combo box).

Hope you understand the technique and how it work even my English is not very good.

Share this Question
Share on Google+
11 Replies


100+
P: 759
Hi again, eneyardi.
Fortunately for you I know what you wish from this thread:
How to shorten codes that using if, else and end if?
Because from actual thread I can't understand :).

So, one way to do this is to make a new module (or use an existing one.
In this module delare a PUBLIC variable. Say strName and create a PUBLIC function (say fGetName)
Something like this:
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public strName As String
  4.  
  5. Public Function fGetName() As String
  6.     fGetName = strName
  7. End Function

In your query, in the field ARE criteria row, write: fGetName().

Close the query and the module (of course save the changes).

Now, go to your combo box (in design view) and, under On Click event write this code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ComboName_Click()
  2.     strName = ComboName
  3. End Sub

That must be all to do.
From now to ever, when you select a new name from your combobox this name will be stored in strName variable. So, when you run the query (or something else like a form or a report based on this query) the query itself will apply (from the criteria row) the function fGetName which will return the value stored in strName (the name you preview selected in your combo box).

Hope you understand the technique and how it work even my English is not very good.
Dec 27 '11 #2

TheSmileyCoder
Expert Mod 100+
P: 2,321
That is certainly one way of doing, and under some circumstances can be very usefull.

I will just for good measure mention another way of doing it:

Presume you have your report set up intially to show all records of all employees. You can also choose to open the report and apply the filter at the same time.

This could be done like so, presuming you have a simple combobox (comboSelectName) listing just the name:
Expand|Select|Wrap|Line Numbers
  1. Dim strName as string
  2. strName=Me.ComboSelectName
  3.  
  4. Dim strFilter as string
  5. strFilter="EmployeeName='" & strName & "'"
  6.  
  7. Docmd.OpenReport "NameOfReport",acViewPreview,,strFilter
Now this will open the report in preview mode, with the filter applied. Its a very usefull feature. The same feature can also be used for forms btw.
Dec 27 '11 #3

100+
P: 180
That's exactly what i'm looking for. thank you guys! I'm excited to apply that on my simple program, I'll give you the update later.
Dec 28 '11 #4

100+
P: 180
Expand|Select|Wrap|Line Numbers
  1. Dim strName As String
  2.  strName = Me.Combo238
  3.  
  4.  Dim strFilter As String
  5. strFilter = "EmployeeName='" & strName & "'"
  6.  
  7. DoCmd.OpenForm "list", acNormal, strFilter
Smiley It shows all the records strfilter not working.
The Recordsource of form name (list) is QueryofMasterlist the field EmployeeName = Combo238 rowsource
Dec 28 '11 #5

100+
P: 180
Mihail i followed your instructions but is not working, i don't know what is my fault. The recordsource became blank
Dec 28 '11 #6

100+
P: 759
Remove all information from your database, ZIP it and attache it to your next post.
Before removing information be sure you make a copy of your database (for safe).

I can think about some reasons that my code make an empty record source:
First can be that you don't use PUBLIC variable and/or function. I don't know if you use Option Explicit statement in yours modules.
The second reason can be that your Combo238 has the first column hidden.
I assume that Combo238 is a bound control. If it is not then we are in war with the wind mills.

I have no idea why Smiley's code give you ALL the records ?!?!
Have you an explication, Smiley ? (you know: I try to learn a little bit SQL. Thank you !)
Dec 28 '11 #7

NeoPa
Expert Mod 15k+
P: 31,307
Please review [code] Tags Must be Used. After over a hundred posts you shouldn't still be posting this nonsense so that other people have to go around after you tidying up.

I will simply delete any of your posts I see in future if they contain untagged code.
Dec 28 '11 #8

100+
P: 180
I'm sorry neopa, I'll do that next time.
Dec 28 '11 #9

100+
P: 180
Mihail thanks alot it works now! I only forgot to save. hehehe.
Dec 28 '11 #10

100+
P: 759
So, one query and five lines of code.
Is now enough short for you ? :)

Glad to help you.
Dec 28 '11 #11

100+
P: 180
Yup, it's a big help for me. Because we're using the program in LAN base, interaction speed improves alot. Thank you!
Dec 29 '11 #12

Post your reply

Sign in to post your reply or Sign up for a free account.