Home All Groups Group Topic Archive Search About

Passing a variable to a SQL statement does not work ?

Author
21 Apr 2006 6:21 PM
glenn
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

Author
21 Apr 2006 6:55 PM
Paul
Could "MICROSOFT" be not the same as "Microsoft" (uppercase vs lowercase)?

Show quote
"glenn" wrote:

> 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
>
Author
21 Apr 2006 6:57 PM
glenn
I see what you are saying but no that has no effect on the issue.
glenn

Show quote
"Paul" wrote:

> Could "MICROSOFT" be not the same as "Microsoft" (uppercase vs lowercase)?
>
> "glenn" wrote:
>
> > 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
> >
Author
21 Apr 2006 7:01 PM
Paul
Oh, well

HOw does it fail?

Show quote
"glenn" wrote:

> I see what you are saying but no that has no effect on the issue.
> glenn
>
> "Paul" wrote:
>
> > Could "MICROSOFT" be not the same as "Microsoft" (uppercase vs lowercase)?
> >
> > "glenn" wrote:
> >
> > > 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
> > >
Author
21 Apr 2006 7:43 PM
Marina Levit [MVP]
'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.

Show quote
"glenn" <gl***@discussions.microsoft.com> wrote in message
news:992E7FD8-0D34-4E44-BA4D-BA35FC5C5EC3@microsoft.com...
> 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
>
Author
24 Apr 2006 12:35 PM
Paul Clement
On Fri, 21 Apr 2006 11:21:01 -0700, glenn <gl***@discussions.microsoft.com> wrote:

¤ 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)

AddThis Social Bookmark Button