list boxes --> query

A

Anakin Moonwalker

Hi guys,

I have 2 questions. I have a list box called "my_list_box" that has only one
column (the contents are "apple", "ball", "cat", "dog").

1. If the user selects "ball", under what variable (not sure about the term)
is the word "ball" stored in? Is it "my_list_box.txt"?

2. I have four queries, namely apple_query, ball_query, cat_query and
dog_query.
What do I need to do so that when the user selects "cat", the query
"cat_query" will be run?

Please write the answer as simple as possible.

Thanks!
 
J

John Vinson

Hi guys,

I have 2 questions. I have a list box called "my_list_box" that has only one
column (the contents are "apple", "ball", "cat", "dog").

1. If the user selects "ball", under what variable (not sure about the term)
is the word "ball" stored in? Is it "my_list_box.txt"?

Its Value property; that's the default property so you can simply
refer to the name of the control. For example

Dim strSearch As String
strSearch = Me!my_list_box

will put "Cat" into the variable if that's what was selected.
2. I have four queries, namely apple_query, ball_query, cat_query and
dog_query.
What do I need to do so that when the user selects "cat", the query
"cat_query" will be run?

Well... there may be a simpler way to do what I'm guessing you intend.
Do the four queries differ *only* in that one has "Cat", another
"Apple", and so on, as criteria? If so, you only need ONE query with a
criterion of

=[Forms]![My_form_name]![my_list_box]

Note that it's VERY rarely necessary or appropriate to "open a query"
in order to look at the results. Query (and table) datasheets are best
kept for debugging, or for "quick and dirty" searches, and not shown
to users. You may want to consider instead basing a Form (or Report,
for printing) on the query, and open *that* instead.

If you in fact have four *different* queries - different in structure,
not just in criteria - post back and explain; what you ask can be
done, but it's a bit more work.


John W. Vinson[MVP]
 
U

user_hj

hi£¬Anakin£¡

The first answer is, the result is stored in the control's value
property.

That means, we can use the code as follow to get what we've selected:

****************************************
Private sub my_list_box_Afterupdate()
Messagebox my_list_box.value
End Sub
****************************************

The second answer is, you can write the code with the listbox's
afterupdate
event like this:

****************************************
Private sub my_list_box_Afterupdate()
select case my_list_box
case "apple"
DoCmd.RunSQL apple_query
case "ball"
DoCmd.RunSQL ball_query
case "cat"
DoCmd.RunSQL cat_query
case "dog"
DoCmd.RunSQL dog_query
end select
End Sub
****************************************

I hope it's useful to you!

user_hj 2005-9-13
 
Top