Passing a variable to a SQL statement does not work ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi folks,

The problem I have is that a query string works if hard-coded but
if I pass a variable to it, it does not work as shown here.

Dim queryString, q1, q2 As String

This works:
querystring="SELECT * FROM USERS WHERE CNAME = 'MICROSOFT'"

This does not work:
Dim var as string
var = "Microsoft"
querystring="SELECT * FROM USERS WHERE CNAME = '" & var & "'"

I have done a Response.Write (q1) and Response.Write (q2) where
q1 is a hard-coded SQL statement and where q2 passes a variable to a SQL
string

The values written to the screen are exactly the same.

Why does the hard-coded version work and the var version not work?

Thanks for any clues.
glenn
 
'does not work' is meaningless. What exactly is happening? Are you getting
error messages? If so, what are they? On what line (show pertinent code)?
What do you expect to happen, vs exactly what it is that is happening?

Why don't you show us your complete code, in the example you posted you are
never using variables q1 and q2.
 
¤ Hi folks,
¤
¤ The problem I have is that a query string works if hard-coded but
¤ if I pass a variable to it, it does not work as shown here.
¤
¤ Dim queryString, q1, q2 As String
¤
¤ This works:
¤ querystring="SELECT * FROM USERS WHERE CNAME = 'MICROSOFT'"
¤
¤ This does not work:
¤ Dim var as string
¤ var = "Microsoft"
¤ querystring="SELECT * FROM USERS WHERE CNAME = '" & var & "'"
¤
¤ I have done a Response.Write (q1) and Response.Write (q2) where
¤ q1 is a hard-coded SQL statement and where q2 passes a variable to a SQL
¤ string
¤
¤ The values written to the screen are exactly the same.
¤
¤ Why does the hard-coded version work and the var version not work?

What type of database are you working with?


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Back
Top