Results 1 to 6 of 6

Thread: APOSTROPHE BAD, PLEASE HELP ASAP!

  1. #1

    Thread Starter
    Addicted Member JasonGS's Avatar
    Join Date
    May 2000
    Location
    California
    Posts
    155
    Using VB6/ADO/Access MDB file...

    I have a table that contains a field that [in some cases] will contain an apostrophe ' and when I try to query for the name O'BRIEN for instance with the following code...
    Code:
    adoRset.Open "SELECT * FROM table WHERE lastname = '" & txtLastName.Text & "' ORDER BY lastname", adoConn
    I get an error of Syntax error (missing operator) in query expression 'employee LIKE '%O'BRIEN%''.... any ideas how to get around this problem?

  2. #2
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482

  3. #3
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    You could scan your string for apostrophe's and, if you find one, use an alternative statement.

    adoRset.Open "SELECT * FROM table WHERE lastname = " & """" & txtLastName.Text & """" & " ORDER BY lastname", adoConn

    is equivalent to what you're using. Of course, if there are any names with quotes in them, you'll be hosed...

  4. #4
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    All you have to do is replace the apostrophies with 2 apostrophies (not a quote) (ex '='')

    just use the VB6 REPLACE function

    Code:
    adoRset.Open "SELECT * FROM table WHERE lastname = '" & replace(txtLastName.Text, "'", "''") & "' ORDER BY lastname", adoConn
    it works every time

  5. #5
    Frenzied Member
    Join Date
    Aug 1999
    Location
    Santa Clara, Ca , 95058
    Posts
    1,105
    Well, between Tom's answer and mine, there's no need to go to the Microsoft URL...


  6. #6

    Thread Starter
    Addicted Member JasonGS's Avatar
    Join Date
    May 2000
    Location
    California
    Posts
    155
    Thank you all for your help. Go figure why 'O''Brien' fixed the problem, but who am I to critique it?!?

    Thanks again!

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