Monday, 9 September 2013

VBA-Query Loop - Unable to convert to Form reference

VBA-Query Loop - Unable to convert to Form reference

All,
I've tried searching through the site and now, I am unable to find an
answer to this question. If someone could point me to the right direction-
I'd appreciate it.
I have a form with 10 combo-boxes. Once user selects a value in CB1- CB2
becomes visible and active.
I am trying to run a dynamic query- where the selection of the combo-box
is used to select a column from my table called "dbo_animals"
To elaborate: CB1 contains the following values -
elephant, giraffe, bufffalo, tiger, lion
Once the user selects elephant in CB1, CB2 becomes active and I select
tiger next. So the query becomes like. The process can go on and the user
can select up to 10 animals
**SELECT elephant, tiger FROM dbo_animals**
Problem:
I am able to create the query with string manipulation- Unfortunately due
to the way the loop through controls is set up- The query is unable to
convert the text into a reference
If I hard code it as tempquery = [Form]![Animal Finder]![CB1]
MsgBox(tempquery) it looks like
Elephant
This is how I want. But, since i am looping through CB controls, I have it
set up as
tempquery = "[Form]![Animal Finder]![CB" & i & "]"
and this shows up as
[Form]![Animal Finder]![CB1]
thereby giving me an error saying that the reference is not valid. Which
makes sense, since it is unable to evaluate the text as a reference.
How can I fix this? Or how do I correct the text into a reference?

No comments:

Post a Comment