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

Pass Variables in SQL WHERE clause

Status
Not open for further replies.

alexisa

MIS
Nov 20, 1998
4
US
I need to be able to run a SQL query that will look something like this:<br>
SELECT * FROM
WHERE Field= (variable)<br>
That is pass a variable to the WHERE clause.<br>
assuming that I have a variable already present and that it holds the value of the a &quot;field&quot;.<br>
What is the CORRECT SYNTAX?<br>
Any help will be great<br>
Thanks<br>
Alexis <p>Alexis<br><a href=mailto:alexisale@msn.com>alexisale@msn.com</a><br><a href= > </a><br>
 
Hi alexisa,<br>
<br>
Below is a sample code:<br>
<br>
Dim strWhere as string<br>
Dim strSQL as string<br>
<br>
strWhere = text1.text 'Take in criteria from text box<br>
<br>
strSQL = &quot;SELECT * FROM
WHERE Field=&quot;<br>
strSQL = strSQL & strWhere<br>
<br>
............<br>
<br>
<br>
Obviuosly you need to open a connection to a database and bring back a recordset. If you are not sure how to do this let me know and I'll post the code.<br>
<br>
HTH<br>
<br>
C<br>

 
I believe that the syntax is different for string vs. numeric fields (possibly dependent on the database).<br>
<br>
For numeric fields:<br>
&quot;Select * From ..... Where myFld = &quot; & NumericField<br>
<br>
For string fields:<br>
&quot;Select * From ..... Where myFld = '&quot; & StringField & &quot;'&quot;<br>
where you're surrounding your data with single quotes.<br>
<br>
For date fields (and Access):<br>
&quot;Select * From ..... Where myFld = #&quot; & DateField & &quot;#&quot;<br>
<br>

 
VB400 is correct, you do need the double quotes around the string. However, the # sign is not needed for dates (outside of access)<br>
<br>
C
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top