...

View Full Version : Passing variable to page with form to an sql statement



briintex1
03-06-2006, 07:12 PM
Hello I am passing variable from one page, to another using a form. My problem is that I need it to be put inside an sql statement. This is the code for the page and the sql statement is at the bottom of this post. Along with the error that I am recieving.


<%
'Create a variable named vNum
Dim Vnum

'Read the search form variable. We will use CStr
'to make sure we are getting a string.
Vnum = CStr(Request("voucher_number"))

'Create SQL connection
set Connect = Server.CreateObject("ADODB.Connection")
Connect.Open "Provider=sqloledb;Server=serverIP;Database=db;UID=id;PWD=pw;"

'Create an ADO recordset object
Set objRS = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT Number FROM Cash where Number like %Vnum"

'Print out the value of SearchString
Response.Write(Vnum)
'Assign recordset types
objRS.CursorType=2
objRS.LockType=3

'Open the recordset with the SQL query
objRS.Open strSQL, Connect

'Loop the recordset until end of file
Do While not objRS.EOF
'Write the HTML to display the current record in the recordset
Response.Write ("<br><br><table width='100%' border = '3'>")
Response.Write (objRS("Vnum"))

objrs.MoveNext
Loop
objRS.Close
set objRS = Nothing
set connect = nothing
%>


My error on my page seems to be at this spot in the code
[
'Initialise the strSQL variable with an SQL statement to query the database
strSQL = "SELECT Number FROM Voucher where Number like %Vnum"
[/code]

What I am doing is getting vnum from the url and trying to put it in the select statement. But I tend to get this error
Line 1: Incorrect syntax near 'Vnum'.
/here/view_page.asp, line 28

Does anyone have any suggestions on how to fix this issue?

Archangel
03-06-2006, 08:42 PM
Syntax is wrong...try this


strSQL = "SELECT Number FROM Cash where Number like %" & Vnum

Roelf
03-06-2006, 09:26 PM
Doesn't the LIKE operator only work on character fields? If so, it should be:

strSQL = "SELECT Number FROM Cash where Number like '%" & Vnum & "'"



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum