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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using "OR" in an SQL select statment in VB6 (Access database)

Status
Not open for further replies.

LesMartin

Programmer
Sep 23, 2003
11
GB
Hi Guys,

Can any one tell me whats wrong with my syntax in my Select statment below.
It runs if i just query field_1 on its own, but when I add the OR it wont go??
Any suggestions are very welcome.
Thanks

Set rs2 = db2.OpenRecordset("SELECT * " & _
"FROM table1 " & _
"WHERE field_1 = " & lngID & _ " or field_2 = " & lngID)
 
Looks fine to me. You might want to try
Code:
"WHERE (Field_1 = " & lngID & ") OR (Field_2 = " & lngID & ")"

Does it work if you only do "WHERE Field_2 = " & lngID?

Andy
"Logic is invincible because in order to combat logic it is necessary to use logic." -- Pierre Boutroux
"Why does my program keep showing error messages every time something goes wrong?"
 
The underscore charater can sometimes cause a problem. You can qualify the field names with square brackets (thus eliminating the need to deal with the underscores) or remove them. Try,

Code:
"("SELECT * FROM table1 "WHERE ([Field_1] = " & lngID & ") OR ([Field_2] = " & lngID & ")"

Take Care,

zemp

"Show me someone with both feet on the ground and I will show you someone who can't put their pants on."
 
Thanks Zemp,
I didnt realise this with the underscore, I will bear that in mind when naming fields in my databases in future.
Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top