Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a database table which contains client information. I have created a form where you use a dropdown box to choose a field to search by, then type the criteria in a box. There is only one box for the criteria.
I am trying to create a query in Access which will perform the search and return the results. I can easily make one which will return accurate results if I hard code the field name 'Address' in for instance, ie using '... where Address = txtcriteria'. The difficult part is though, I don't want to hard code the field name in. I want it to be read from the forms drop down box. I have tried SQL such as '...where cbofield = txtcriteria'. This doesn't work though, and I get no results returned.
I know I could do it in VBA by reading the value of the dropdown box and running a different SQL statment for each possible option, but surely it can be done in 1 SQL statment where this reads the value? All I need to do is use the value of a combo box instead of hard coding a field name...
Any ideas?

If I understand you correctly, you want the query to get the value from the combo box in the form. In the query, use [forms]![formname]![comboname] in the criteria portion for the field. Is this what you are looking for?

Thats what I tried, although it does not work :(
I resorted to using VBA in the end though as that allows variable names and I can do it that way. :P

Use this:
" ... WHERE cbofield = " & Combo1.List(Combo1.ListIndex)
Combo1.listindex returns a number that indicates the item on the list that is selected.
Stuart

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |