+ Reply to Thread
Results 1 to 8 of 8

Thread: vba sql special characters

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    vba sql special characters

    Hi all,

    i'm trying to input a string into a table in a database via vba from an excel sheet, my problem is that every so often a string will have a name like " mr 'ben' " which errors as ' is a special character

    what do i do?

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: vba sql special characters

    Replace ' with '' before you try and add it.

  3. #3
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: vba sql special characters

    unfortunately i'm reading the strings in from a file, there are many thousands of them so going through 1 by one would not really be an option!

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: vba sql special characters

    Where did I suggest you should do that? You do it in code.

  5. #5
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: vba sql special characters

    what like:

    VALUES ("" & Cells(y, x).Value & "",
    ?
    Last edited by romperstomper; 11-08-2010 at 11:16 AM. Reason: correct tags

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: vba sql special characters

    More like:
    VALUES (" & Replace(Cells(y, x).Value, "'","''") & "
    assuming that's how you are doing the update, or do a global replace on the whole sheet first.

  7. #7
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: vba sql special characters

    hmm that gives me a missing operator error

    : syntax error (missing operator) in query expression 'bbc 1'

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: vba sql special characters

    Typo as you need quotes around the values:
    VALUES ('" & Replace(Cells(y, x).Value, "'","''") & "'"

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0