473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Combo Box as Criteria for Select Query

Newbie needing some help. I have a tblParticipants. The fields
include gender, education_level, income, occupation etc., I'm trying to
create a form where a user can run a query from the form and just
choose the appropriate criterias from the combo boxes to get the
results. I also want the query to run even if there is not a value in
all the combo boxes ie., i want just all males with income level of
over $100,000...Any insights or help would be greatly appreciated.
Thanks.

Jul 25 '06 #1
5 3673
jj***********@yahoo.com wrote:
Newbie needing some help. I have a tblParticipants. The fields
include gender, education_level, income, occupation etc., I'm trying to
create a form where a user can run a query from the form and just
choose the appropriate criterias from the combo boxes to get the
results. I also want the query to run even if there is not a value in
all the combo boxes ie., i want just all males with income level of
over $100,000...Any insights or help would be greatly appreciated.
Thanks.
I would create your combo boxes that will filter your record selection.
I assume you know how to create the combo boxes and supply the data.

I also assume you can do a little bit of vba coding.

Higlight all the combos for filtering, select the AfterUpdate event row
and enter
=MakeFilter()

Create a command button to reset the filter. This will clear out the
combo boxes and display all records. In the OnClick event, click on the
triple dot at end of row, select EventProcedure, and enter something like
Dim blnOK As Boolean
Me.ComboGender = Null
Me.ComboIncome = Null
'...null out rest of combos
blnOK = MakeFilter()

Now create your MakeFilter function. Create your filter with a series
of IF statements

Private Function MakeFilter() As Boolean
'if you are using string values in the combo, surround
'combo value with '' or "" (gender). If numerica value (due to
'lookup tables) don't surround (Occupation)
Dim strFilter As String
If Not IsNull(Me.ComboGender) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Gender = '" & Me.ComboGender & "' And "
Endif

If Not IsNull(Me.ComboOccupation) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Occupation = " & ComboOccupation & " And "
Endif
...continue this method for rest of combos

'now remove word AND at end of strFilter
if strFilter "" Then 'not all combos were null
strFilter = Left(strFilter,len(strFilter)-5)
Endif

'now set filter
Me.Filter = strFilter 'strFilter looks like Where statement
'without the word "Where"
'now turn on filter
Me.FilterOn = True

MakeFilter = True
End Function

This creates the filter and is called anytime you change a value in the
combos or resetting the filter.

Jul 25 '06 #2
salad wrote:
jj***********@yahoo.com wrote:
Newbie needing some help. I have a tblParticipants. The fields
include gender, education_level, income, occupation etc., I'm trying to
create a form where a user can run a query from the form and just
choose the appropriate criterias from the combo boxes to get the
results. I also want the query to run even if there is not a value in
all the combo boxes ie., i want just all males with income level of
over $100,000...Any insights or help would be greatly appreciated.
Thanks.
I would create your combo boxes that will filter your record selection.
I assume you know how to create the combo boxes and supply the data.

I also assume you can do a little bit of vba coding.

Higlight all the combos for filtering, select the AfterUpdate event row
and enter
=MakeFilter()

Create a command button to reset the filter. This will clear out the
combo boxes and display all records. In the OnClick event, click on the
triple dot at end of row, select EventProcedure, and enter something like
Dim blnOK As Boolean
Me.ComboGender = Null
Me.ComboIncome = Null
'...null out rest of combos
blnOK = MakeFilter()

Now create your MakeFilter function. Create your filter with a series
of IF statements

Private Function MakeFilter() As Boolean
'if you are using string values in the combo, surround
'combo value with '' or "" (gender). If numerica value (due to
'lookup tables) don't surround (Occupation)
Dim strFilter As String
If Not IsNull(Me.ComboGender) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Gender = '" & Me.ComboGender & "' And "
Endif

If Not IsNull(Me.ComboOccupation) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Occupation = " & ComboOccupation & " And "
Endif
...continue this method for rest of combos

'now remove word AND at end of strFilter
if strFilter "" Then 'not all combos were null
strFilter = Left(strFilter,len(strFilter)-5)
Endif

'now set filter
Me.Filter = strFilter 'strFilter looks like Where statement
'without the word "Where"
'now turn on filter
Me.FilterOn = True

MakeFilter = True
End Function

This creates the filter and is called anytime you change a value in the
combos or resetting the filter.
I took the above steps and when I click on the button it doesn't run
the query or anything. It just clears the selection made in the combo
boxes. Here is the code that I have for the form. Thanks.

Private Sub Command6_Click()
Dim blnOK As Boolean
Me.Combo0 = Null
Me.Combo2 = Null
blnkOK = MakeFilter()
End Sub

Private Function MakeFilter() As Boolean
Dim strFilter As String
If Not IsNull(Me.Combo0) Then
strFilter = strFilter & _
"Gender = '" & Me.Combo0 & "' And "
End If
If Not IsNull(Me.Combo2) Then
strFilter = strFilter & _
"Occupation = " & Me.Combo2 & " And "
End If
If strFilter "" Then
strFilter = Left(strFilter, Len(strFilter) - 5)
End If
Me.Filter = strFilter
Me.FilterOn = True
MakeFilter = True
End Function

Jul 26 '06 #3
jj***********@yahoo.com wrote:
salad wrote:
>>jj***********@yahoo.com wrote:
>>>Newbie needing some help. I have a tblParticipants. The fields
include gender, education_level, income, occupation etc., I'm trying to
create a form where a user can run a query from the form and just
choose the appropriate criterias from the combo boxes to get the
results. I also want the query to run even if there is not a value in
all the combo boxes ie., i want just all males with income level of
over $100,000...Any insights or help would be greatly appreciated.
Thanks.

I would create your combo boxes that will filter your record selection.
I assume you know how to create the combo boxes and supply the data.

I also assume you can do a little bit of vba coding.

Higlight all the combos for filtering, select the AfterUpdate event row
and enter
=MakeFilter()

Create a command button to reset the filter. This will clear out the
combo boxes and display all records. In the OnClick event, click on the
triple dot at end of row, select EventProcedure, and enter something like
Dim blnOK As Boolean
Me.ComboGender = Null
Me.ComboIncome = Null
'...null out rest of combos
blnOK = MakeFilter()

Now create your MakeFilter function. Create your filter with a series
of IF statements

Private Function MakeFilter() As Boolean
'if you are using string values in the combo, surround
'combo value with '' or "" (gender). If numerica value (due to
'lookup tables) don't surround (Occupation)
Dim strFilter As String
If Not IsNull(Me.ComboGender) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Gender = '" & Me.ComboGender & "' And "
Endif

If Not IsNull(Me.ComboOccupation) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Occupation = " & ComboOccupation & " And "
Endif
...continue this method for rest of combos

'now remove word AND at end of strFilter
if strFilter "" Then 'not all combos were null
strFilter = Left(strFilter,len(strFilter)-5)
Endif

'now set filter
Me.Filter = strFilter 'strFilter looks like Where statement
'without the word "Where"
'now turn on filter
Me.FilterOn = True

MakeFilter = True
End Function

This creates the filter and is called anytime you change a value in the
combos or resetting the filter.


I took the above steps and when I click on the button it doesn't run
the query or anything. It just clears the selection made in the combo
boxes. Here is the code that I have for the form. Thanks.

Private Sub Command6_Click()
Dim blnOK As Boolean
Me.Combo0 = Null
Me.Combo2 = Null
blnkOK = MakeFilter()
End Sub

Private Function MakeFilter() As Boolean
Dim strFilter As String
If Not IsNull(Me.Combo0) Then
strFilter = strFilter & _
"Gender = '" & Me.Combo0 & "' And "
End If
If Not IsNull(Me.Combo2) Then
strFilter = strFilter & _
"Occupation = " & Me.Combo2 & " And "
End If
If strFilter "" Then
strFilter = Left(strFilter, Len(strFilter) - 5)
End If
Me.Filter = strFilter
Me.FilterOn = True
MakeFilter = True
End Function
The code you passed may or may not work. I was guessing at the form's
field names.

If the above code is working, it should allow you to view all records as
it clears/resets the filter for the form.

Here is something you can do. Open up the code module containing
MakeFilter. On the vertical bar of the code window on the left hand
side next to
Private Function MakeFilter()
click it. A red dot should appear. This will step you thru your code.
As you step thru the code you can put your cursor over variables to
discover the value of it. This may show you whether or not the code is
executed and what happens when it is executed.
Jul 26 '06 #4

salad wrote:
jj***********@yahoo.com wrote:
salad wrote:
>jj***********@yahoo.com wrote:

Newbie needing some help. I have a tblParticipants. The fields
include gender, education_level, income, occupation etc., I'm trying to
create a form where a user can run a query from the form and just
choose the appropriate criterias from the combo boxes to get the
results. I also want the query to run even if there is not a value in
all the combo boxes ie., i want just all males with income level of
over $100,000...Any insights or help would be greatly appreciated.
Thanks.
I would create your combo boxes that will filter your record selection.
I assume you know how to create the combo boxes and supply the data.

I also assume you can do a little bit of vba coding.

Higlight all the combos for filtering, select the AfterUpdate event row
and enter
=MakeFilter()

Create a command button to reset the filter. This will clear out the
combo boxes and display all records. In the OnClick event, click on the
triple dot at end of row, select EventProcedure, and enter something like
Dim blnOK As Boolean
Me.ComboGender = Null
Me.ComboIncome = Null
'...null out rest of combos
blnOK = MakeFilter()

Now create your MakeFilter function. Create your filter with a series
of IF statements

Private Function MakeFilter() As Boolean
'if you are using string values in the combo, surround
'combo value with '' or "" (gender). If numerica value (due to
'lookup tables) don't surround (Occupation)
Dim strFilter As String
If Not IsNull(Me.ComboGender) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Gender = '" & Me.ComboGender & "' And "
Endif

If Not IsNull(Me.ComboOccupation) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Occupation = " & ComboOccupation & " And "
Endif
...continue this method for rest of combos

'now remove word AND at end of strFilter
if strFilter "" Then 'not all combos were null
strFilter = Left(strFilter,len(strFilter)-5)
Endif

'now set filter
Me.Filter = strFilter 'strFilter looks like Where statement
'without the word "Where"
'now turn on filter
Me.FilterOn = True

MakeFilter = True
End Function

This creates the filter and is called anytime you change a value in the
combos or resetting the filter.

I took the above steps and when I click on the button it doesn't run
the query or anything. It just clears the selection made in the combo
boxes. Here is the code that I have for the form. Thanks.

Private Sub Command6_Click()
Dim blnOK As Boolean
Me.Combo0 = Null
Me.Combo2 = Null
blnkOK = MakeFilter()
End Sub

Private Function MakeFilter() As Boolean
Dim strFilter As String
If Not IsNull(Me.Combo0) Then
strFilter = strFilter & _
"Gender = '" & Me.Combo0 & "' And "
End If
If Not IsNull(Me.Combo2) Then
strFilter = strFilter & _
"Occupation = " & Me.Combo2 & " And "
End If
If strFilter "" Then
strFilter = Left(strFilter, Len(strFilter) - 5)
End If
Me.Filter = strFilter
Me.FilterOn = True
MakeFilter = True
End Function
The code you passed may or may not work. I was guessing at the form's
field names.

If the above code is working, it should allow you to view all records as
it clears/resets the filter for the form.

Here is something you can do. Open up the code module containing
MakeFilter. On the vertical bar of the code window on the left hand
side next to
Private Function MakeFilter()
click it. A red dot should appear. This will step you thru your code.
As you step thru the code you can put your cursor over variables to
discover the value of it. This may show you whether or not the code is
executed and what happens when it is executed.
I don't get anything like that when I open the code module at all. On
the left hand side I have :
(General)
Combo0
Combo2
Command6
Detail
Form

On the right hand side there is:
(Declarations)
MakeFilter

I'm using Access 2002 if that helps any. Here is all that I want the
user to be able to do:

I want a user to select different criterias from combo boxes--such as
gender, occupation, income. then click on a button to run that query.
what is returned to the users is everything from the participants table
that match those criterias. Am I going about this all wrong? Any
insights would be greatly appreciated. I'm new to all this and just
started to get my feet wet. Thanks.

Jul 26 '06 #5
jj***********@yahoo.com wrote:
salad wrote:
>>jj***********@yahoo.com wrote:

>>>salad wrote:
jj***********@yahoo.com wrote:
>Newbie needing some help. I have a tblParticipants. The fields
>include gender, education_level, income, occupation etc., I'm trying to
>create a form where a user can run a query from the form and just
>choose the appropriate criterias from the combo boxes to get the
>results. I also want the query to run even if there is not a value in
>all the combo boxes ie., i want just all males with income level of
>over $100,000...Any insights or help would be greatly appreciated.
>Thanks.
>

I would create your combo boxes that will filter your record selection.
I assume you know how to create the combo boxes and supply the data.

I also assume you can do a little bit of vba coding.

Higlight all the combos for filtering, select the AfterUpdate event row
and enter
=MakeFilter()

Create a command button to reset the filter. This will clear out the
combo boxes and display all records. In the OnClick event, click on the
triple dot at end of row, select EventProcedure, and enter something like
Dim blnOK As Boolean
Me.ComboGender = Null
Me.ComboIncome = Null
'...null out rest of combos
blnOK = MakeFilter()

Now create your MakeFilter function. Create your filter with a series
of IF statements

Private Function MakeFilter() As Boolean
'if you are using string values in the combo, surround
'combo value with '' or "" (gender). If numerica value (due to
'lookup tables) don't surround (Occupation)
Dim strFilter As String
If Not IsNull(Me.ComboGender) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Gender = '" & Me.ComboGender & "' And "
Endif

If Not IsNull(Me.ComboOccupation) Then
'assumes fld nm is gender and stuffing an M or F
strFilter = strFilter & _
"Occupation = " & ComboOccupation & " And "
Endif
...continue this method for rest of combos

'now remove word AND at end of strFilter
if strFilter "" Then 'not all combos were null
strFilter = Left(strFilter,len(strFilter)-5)
Endif

'now set filter
Me.Filter = strFilter 'strFilter looks like Where statement
'without the word "Where"
'now turn on filter
Me.FilterOn = True

MakeFilter = True
End Function

This creates the filter and is called anytime you change a value in the
combos or resetting the filter.
I took the above steps and when I click on the button it doesn't run
the query or anything. It just clears the selection made in the combo
boxes. Here is the code that I have for the form. Thanks.

Private Sub Command6_Click()
Dim blnOK As Boolean
Me.Combo0 = Null
Me.Combo2 = Null
blnkOK = MakeFilter()
End Sub

Private Function MakeFilter() As Boolean
Dim strFilter As String
If Not IsNull(Me.Combo0) Then
strFilter = strFilter & _
"Gender = '" & Me.Combo0 & "' And "
End If
If Not IsNull(Me.Combo2) Then
strFilter = strFilter & _
"Occupation = " & Me.Combo2 & " And "
End If
If strFilter "" Then
strFilter = Left(strFilter, Len(strFilter) - 5)
End If
Me.Filter = strFilter
Me.FilterOn = True
MakeFilter = True
End Function

The code you passed may or may not work. I was guessing at the form's
field names.

If the above code is working, it should allow you to view all records as
it clears/resets the filter for the form.

Here is something you can do. Open up the code module containing
MakeFilter. On the vertical bar of the code window on the left hand
side next to
Private Function MakeFilter()
click it. A red dot should appear. This will step you thru your code.
As you step thru the code you can put your cursor over variables to
discover the value of it. This may show you whether or not the code is
executed and what happens when it is executed.


I don't get anything like that when I open the code module at all. On
the left hand side I have :
(General)
Combo0
Combo2
Command6
Detail
Form

On the right hand side there is:
(Declarations)
MakeFilter

I'm using Access 2002 if that helps any. Here is all that I want the
user to be able to do:

I want a user to select different criterias from combo boxes--such as
gender, occupation, income. then click on a button to run that query.
what is returned to the users is everything from the participants table
that match those criterias. Am I going about this all wrong? Any
insights would be greatly appreciated. I'm new to all this and just
started to get my feet wet. Thanks.
OK. You could scroll thru the code till you find MakeFilter or click on
MakeFilter in the dropdown. Then to the left of the word
Function
on the window's vertical bar click on the bar. Close the code window on
open the form.

I don't know your programming skill. I recommend books like the MS
StepByStep to get you started in learning event programming. If link
wraps, see if turning of wordwrap assists. These books step you through
coding. If you'll remain with Access, maybe get the Developer's
Handbook by Getz for more advanced stuff.

http://www.amazon.com/gp/product/073...e=UTF8&s=books
http://www.amazon.com/gp/product/073...e=UTF8&s=books
Jul 26 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cindi Simonson | last post by:
Hi, I have a form with a combo box containing 4 columns of data. The form also contains 3 print buttons where the goal is to open 3 different reports according to the value in one of the...
1
by: BigJay | last post by:
I am 1. trying to have a combobox used as a selector to display records in a subform. and not sure on how to get selected info into subform.the combo is populated but can not get subform updated...
4
by: meganrobertson22 | last post by:
Hi Everyone- I have a question about how to add and then use the "All" selection in a combo box. I am trying to figure out how to: (1) add "All" as a selection to a combo box and then (2)...
3
by: Stewart | last post by:
Hi all! My (relatively small) database holds data on staff members and the projects (services) that they are assigned to. In my form frmStaff, I have a list of staff members - it is a...
10
by: motessa | last post by:
Hello All, I am new to Access and have been looking for answers to my problem on the internet and have not found it yet. I hope someone can give me a hint. Thanks so much. I have a form...
3
by: hmiller | last post by:
Hey everyone, I am having a hell of a time trying to set this menu system up. Here's what I'm trying to do. Combo Box One; is populated by names under properties "row source" "Phase 1"...
2
by: visionstate | last post by:
Hi there, I am working on a form that uses 3 text boxes and 3 combo boxes. When any data is entered into any of these, I click a command button and this requeries a sub query in the form and...
7
by: Ceebaby via AccessMonster.com | last post by:
Hi All Here's hoping someone can help me with this. I have a report based on a query where the criteria for 4 of the fields is set from an unbound form. I want the user to be able to select any...
2
by: cmartin1986 | last post by:
First of all I want to thank all of you that have helped me in the past this is an awesome fourm. My problem today is I have a database that builds charts that are viewed by a large group every...
2
by: franc sutherland | last post by:
Hi, I am using Access 2003. I am having a problem with a pair of combo boxes. I want the second one (cbo_service) to have it's row source limited by the value I select in the first one...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.