Results 1 to 7 of 7

Thread: Access/VB Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Snohomish, WA
    Posts
    4

    Unhappy

    I have an .asp page that gathers data from users and enters it into an Access database. The input fields are 'text' and the database fields are 'text'. But if a user puts some sort of punctuation such as an appostrophy or comma Access returns an error: "Syntax error (missing operator) in query expression "
    Do I need to replace select characters with ascii?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try putting quotes (") around the string they supply.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Snohomish, WA
    Posts
    4

    Tried quotes...

    Here's the code:
    strSQL = "UPDATE Customer SET WirelessNumber = '" & Input_text & "'

    where whould additional quotes go?

    The problem occurs when Input_text has a quote, apostrophy or comma. Something like 'today's entry'.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Try surrounding it in double-quotes.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Snohomish, WA
    Posts
    4

    Where do the quotes go?

    As you can see strSQL is a concatenation of strings and variables. I can't double quote the whole thing because the concatenations won't work.
    I tried triple quoting the one variable I'm having trouble with (" " " & variable & " " ") but that results in a MS Access error.
    I also tried enclosing the single quotes in double quotes (" ' " & variable & " ' ") but VBScript treats everything after the first single quote as a remark or comment.

    The SQL works fine if I enter it directly into Access.

    H
    e
    l
    p
    .
    .
    .

  6. #6
    Guest
    do this:

    Input_text = Replace (Input_text, "'", "''")

    strSQL = "UPDATE Customer SET WirelessNumber = '" & Input_text & "'

    It doesn't look proper here so I'll describe what I'm doing. I'm replacing each single quote in Input_text with two single quotes.

    I'm not sure what all the reserve characters in Access are, but you may also want to just create a function that will handle all reserved characters.

    An example (for SQL server) is:
    Code:
    Input_text = FixStr(Input-text)
    
    Function FixStr (strSQL as string)
    
    FixStr = Replace(strSQL, "'", "''") 'replace single quote with two single quotes
    FixStr = Replace(FixStr, "%", "[%]") 'use %as literal
    FixStr = Replace(FixStr, "_", "[_]") 'use underscore as literal
    
    End Function
    It also looks like you don't have the last single quote in

    strSQL = "UPDATE Customer SET WirelessNumber = '" & Input_text & "'

    inside the double quote, it should be
    Code:
    .....Input_text & '"

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Snohomish, WA
    Posts
    4

    Talking That did it.

    Thanks! That fixed it. Your explanation was quite clear. I appreciate your help.

    "..So Arnie says to Jack..."

Posting Permissions

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



Click Here to Expand Forum to Full Width