Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Application Security Testing: An Integral Part of DevOps

Using variable in SQL WHERE clause

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.

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.



Thanks for your registration, follow us on our social networks to keep up-to-date