+ Reply to Thread
Results 1 to 9 of 9

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
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: vba sql special characters

    Replace ' with '' before you try and add it.
    Remember what the dormouse said
    Feed your head

  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
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    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:

    Please Login or Register  to view this content.
    ?
    Last edited by romperstomper; 11-08-2010 at 12:16 PM. Reason: correct tags

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: vba sql special characters

    More like:
    Please Login or Register  to view this content.
    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
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: vba sql special characters

    Typo as you need quotes around the values:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    08-26-2014
    Location
    Auckland
    MS-Off Ver
    Office 2010
    Posts
    1

    Re: vba sql special characters

    What if I don't want to remove the special character? I have customers names with ' in, so need to keep this. I also have free text with words like can't, to the ' causes an error. How do a do the VBA to handle this? this is what I have currently, which is part of the code I use to upload from excel to MS SQL. How do I alter this to handle the ' ?

    conn.Execute "insert into dbo.Comments (CompanyID, ContactID, ContactName, EnteredBy, Source, Comments) values ('" & sCompanyID & "', '" & sContactID & "', '" & sContactName & "', '" & sEnteredBy & "', '" & sSource & "', '" & sComments & "')"

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: vba sql special characters

    Nowhere was it suggested you have to remove the apostrophe - you just have to double it in the VALUES statement using code like I posted.

+ 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.6.0 RC 1