Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Dec 23, 1997

Using variable in SQL WHERE clause

Question:
How can I use the contents of a text box as the WHERE clause in a SQL query? "SELECT * FROM Table WHERE Field = Text1.Text" doesn't work.

Answer:
The SQL parser is expecting a string of the form:

  SELECT * FROM Table WHERE Field = 'Value'
if 'Field' contains text data, or
  . . .WHERE Field = Value
if 'Field' is numeric (note the lack of quotes around 'Value' in the latter example).

In order to use a variable (including the value of a control property) in a SQL query, you must insert its value, not its name, into the query string:

  SQL = "SELECT * FROM Table WHERE " & _
        "Field = '" & txtBox.Text & "'"
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap