Results 1 to 6 of 6

Thread: Trouble with strings in SQL Statement

  1. #1

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

    Trouble with strings in SQL Statement

    I'm having a terrible time using strings in my SQL statement. If one of the fields contains a ' then I get an syntax error. I've tried several ways to try an fix this, this is the most recent. Any help with this is EXTREMELY appreciated as I have been stuck on this for 4 days now.
    VB Code:
    1. SQL = "INSERT into " & table & " VALUES ('" _
    2.     & Replace(strFilepath, "'", """") & "','" _
    3.     & Replace(strFileName, "'", """") & "','" _
    4.     & Replace(strFileSize, "'", """") & "','" _
    5.     & Replace(strArtist, "'", """") & "','" _
    6.     & Replace(strTitle, "'", """") & "','" _
    7.     & Replace(strAlbum, "'", """") & "','" _
    8.     & Replace(strFrequency, "'", """") & "','" _
    9.     & Replace(strBitrate, "'", """") & "','" _
    10.     & Replace(strLength, "'", """") & "','" _
    11.     & Replace(strTrack, "'", """") & "')"
    12.  
    13. Cnn.Execute (SQL)

  2. #2
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    I think you're replacing with the wrong characters. Try as in the following example.

    Code:
    Replace(strFilepath, "'", "''") etc...
    The liver is bad. It must be punished.

  3. #3

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

    I've tried that,...

    and I still get the same error.

  4. #4
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    I just noticed...

    There's 2 things wrong with the SQL statement. Firstly, keep the Replace they way it is in my example.

    The syntax eror you're getting is due to you not specifying the fields you want to insert to (just noticed that now. )

    It should be of the form

    Code:
    INSERT INTO tblTableName(Column1,Column2...ColumnN)
    VALUES(Value1,Value2...ValueN)
    The liver is bad. It must be punished.

  5. #5

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

    What's wrong now...Syntax Error

    Ok great! Now I'm getting a syntax error in my SQL statement. Also you don't need the column names unless your adding info to items that are not in order. Anyway, I'm getting a syntax error now. What's wrong with it now?
    VB Code:
    1. 'Use SQL to insert the information in the strings into the table
    2. SQL = "INSERT into " & table & "(FilePath, Filename, Filesize, Artist, Title, Album, Frequency, Bitrate, Length, Track)VALUES ('" _
    3. & Replace(strFilepath, "'", "''") & "','" _
    4. & Replace(strFileName, "'", "''") & "','" _
    5. & Replace(strFileSize, "'", "''") & "','" _
    6. & Replace(strArtist, "'", "''") & "','" _
    7. & Replace(strTitle, "'", "''") & "','" _
    8. & Replace(strAlbum, "'", "''") & "','" _
    9. & Replace(strFrequency, "'", "''") & "','" _
    10. & Replace(strBitrate, "'", "''") & "','" _
    11. & Replace(strLength, "'", "''") & "','" _
    12. & Replace(strTrack, "'", "''") & "')"
    13. Cnn.Execute (SQL)

  6. #6
    Fanatic Member
    Join Date
    Aug 2001
    Location
    Connecticut
    Posts
    855
    should be a space in between:
    Track)VALUES

    btw- I don't know if vb is forgiving on this (using function syntax when nothing is returned). If not, try no parentheses.
    Cnn.Execute SQL
    VB 6.0, Access, Sql server, Asp

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