Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    texas
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Passing variable to page with form to an sql statement

    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.
    Code:
    <%
        '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?
    thanks
    bri

  • #2
    Registered User
    Join Date
    Jan 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Syntax is wrong...try this


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

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Doesn't the LIKE operator only work on character fields? If so, it should be:
    Code:
    strSQL = "SELECT Number FROM Cash where Number like '%" & Vnum & "'"
    I am the luckiest man in the world


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •