Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Variable in VBA 3

Status
Not open for further replies.

jake7363

Technical User
May 31, 2006
56
Hi,
SQL is a bit new for me, so patience is appreciated.

I am trying to create a paramater query that searches eight fields for the same value. I want to do this by entering the data value just once, so the MS Query grid will not apply.

I have surmised that I need to create a variable from the value that is entered for the first field, then assigned to the other 7 fields as well. This is where I am getting stuck. How can I create the variable? Do I do this in SQL or in VBA and pass it to the SQL statement? Or is what I am asking too complicated an operation?

Thanks in advance,
Jake
 
WHERE [parameter value] IN (field1, field2, ..., field8)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Great Post!

Never Thought of that to put the search value on the right side of the equation

But this gives you an <or > query can this be done with a <and > query
 
PHV's code is equivalent to
Code:
WHERE
     [parameter value] = field1
  OR [parameter value] = field2
  OR [parameter value] = field3
   :
  OR [parameter value] = field8
You could change the "OR" to "AND" in that expanded form.

There's also the obscure SQL
Code:
WHERE [parameter value] = ALL (field1, field2, field3, ... field8)
That requires that [parameter value] be equal to ALL the listed values (and of course, that they are equal to each other.)
 
Thanks to all...works like a charm! The options will be helpful as well..
 
Thank's for that bit of info Golom, I've never seen it done that way. It should work great for a search. Gave you a star for that one...
 
i am trying to run this sql and this is the error that i get


SELECT Table1.*
FROM Table1 where 1= all([f1],[f2],[f3])



Syntax error (comma) in query expression '1= all([f1],[f2],[f3])'.

what am i doing wrong?
 
FROM Table1 where [highlight]1[/highlight]= all([f1],[f2],[f3])

I find that weird.....

Try it with a string instead 'AB' for example, in a test table.
 
ItIsHardToProgram
tried with a string it is still no good same error
maybe all does not work in access 2003
 
I'm afraid that Golom's expression works only for subqueries[/s] and not for a list ...
 
I talk about syntax.
I suspect that Golom's code is not only obscure but of more incorrect.
 
i think this should do it

SELECT Table1.*
FROM Table1 where 1 =[f1]=[f2]=[f3]
 
Still waiting on Golom's explanation about how to avoid syntax error with it's suggestion:
Golom said:
WHERE [parameter value] = ALL (field1, field2, field3, ... field8)
 
Try adding "Cast" to datatypes that are numric or dates.

Code:
"SELECT Table1.* FROM Table1 WHERE 'Parameter' IN (sLName, sFName, CAST(iNumberField AS NVARCHAR(10)))
 
My Bad, forgot what forum I was in. Try "CStr"
Code:
"SELECT Table1.* FROM Table1 WHERE 'Parameter' IN (sLName, sFName, CStr(iNumberField))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top