Results 1 to 10 of 10

Thread: Unresolved issue....Strings in SQL Statement

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Question Unresolved issue....Strings in SQL Statement

    Hey, I still can't figure out how to get this to work. I am adding strings into the table in SQL statement. This works fine except there is an error if the string contains a single quote. So this is how it was suggested that I do this, but I'm still getting the error. If anyone can help me out with this it would be greatly appreciated. Thanks.

    VB Code:
    1. SQL = "INSERT into " & table & " VALUES ('" & Replace(strFilepath, "'", "''") & "','" & Replace(strFileName, "'", "''") & "','" & Replace(strFileSize, "'", "''") & "','" & Replace(strArtist, "'", "''") & "','" & strTitle & "','" & Replace(strAlbum, "'", "''") & "','" & Replace(strFrequency, "'", "''") & "','" & Replace(strBitrate, "'", "''") & "','" & Replace(strLength, "'", "''") & "','" & Replace(strTrack, "'", "''") & "')"
    2. Cnn.Execute (SQL)

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Are you sure there is not a single quote in the strTitle string?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Ok, fixed that........but

    Now I'm getting this error: "Error in String Syntax: "Stevie Ray Vaughn'


    Any Idea what the problem is now!????

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    i think having any ' chracters even if they,re doubled up will cause a problem...

    try this

    VB Code:
    1. SQL = "INSERT into " & Table & " VALUES ('" _
    2.     & Replace(strFilepath, "'", """") & "','" _
    3.     & Replace(strFileName, "'", """") & "','" _
    4.     & Replace(strFileSize, "'", """") & "','" _
    5.     & Replace(strArtist, "'", """") & "','" _
    6.     & strTitle & "','" _
    7.     & Replace(strAlbum, "'", """") & "','" _
    8.     & Replace(strFrequency, "'", """") & "','" _
    9.     & Replace(strBitrate, "'", """") & "','" _
    10.     & Replace(strLength, "'", """") & "','" _
    11.     & Replace(strTrack, "'", """") & "')"
    12. Cnn.Execute (SQL)

    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    Junior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    27

    Further 'Single Quote' issues

    I agree with the comment that using variables will solve the situation for basic queries. For example, if I use strText and the variable contains one or more single quote characters then this will pass ok with VB code such as:

    lstrSQL = "SELECT * FROM tblWhatever " & _
    "WHERE ColumnA = ' " & strText & " ' "

    HOWEVER the problem I am now experiencing is that I am getting into stored procedures and passing a variable to a stored procedure no longer disregards the single quote contents of a variable!! So I am back to square one!!

    Basically, the following (derived from above) will no longer work IF one or more single quote character is included:

    lstrSQL = "EXEC sp_procName & " ' " & strText & " ' "

    I know that changing the single quotes here to double quotes via Chr(34), WILL accept my single quote but then I will get a problem if the strText variable contains one or more double quote, so I am no further forward.

    So what other alternatives are there for use with stored procedure??

  6. #6
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    instead of replacing them with double quotes, just remove them, by replacing them with nothing ("") that should do it
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    The reason you are getting the error is as follows.

    The data string you posted = DataText'

    (notice the single quote at the end)

    and when you run the replace it looks like this

    DataText''

    and then you add it to the SQL string as a whole and get

    'DataText'''

    (now 3 single quotes at the end).

    Now to the SQL the string it look like you are trying to pass two strings into one parameter like so:

    'DataText' ' ' (spaces added to show)

    ------------

    Theres your problem, now I need to fix it myself for some code im working on (you helped find my problem early ). When I come up with something Ill add it here. But try adding yet another single quote into the equation (e.g 'DataText'''') that may work but will post later what Ive found.

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    what i do for my sql statements was i made a public function in a module.. it saves space and makes the sql string less crowded

    VB Code:
    1. public function apost(TextString as string) as string
    2.     apost = replace(TextString,"'","''")
    3. end function
    then in code

    Code:
    sSql = "INSERT INTO tblMyTable [Name],[Company],[PhoneNum] VALUES('" & apost(txtName) & "','" & apost(txtCompany) & "','" & txtPhoneNum & "')"

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780
    Ive done some tests now, and found that having the extra ' quote at the end is not the reason as I can add data like that to my database.

    Check your code for something else probly a missing ' somewhere

  10. #10
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    Replace your execute with a debug.print so that you can see the the created SQL statement. Paste that into T-SQL and see if that runs.
    The liver is bad. It must be punished.

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