Link to home
Start Free TrialLog in
Avatar of sglee
sglee

asked on

Error in FindFirst when the string has single quatation

Hi,

 I have an ACCESS program that generates an error when the event name contains a single quote. For example, 2013 X'mas party will cause an error where as 2013 Xmas part will not error.
 Here is the code:

    Set db = CurrentDb()
    Set Rst = db.OpenRecordset("Events", dbOpenDynaset)
    Rst.FindFirst "[Event_Name] = '" & Me!Event_Name & "'"   <----- stops here

 Can you help?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sglee
sglee

ASKER

Jim,
 
 That worked. Thank you.
 I know why my code caused an error (because there was a quote inside two single quotes).
 How come your code works?
it's about encoding of the string delimiter.

the alternative would have been:

    Rst.FindFirst "[Event_Name] = '" & replace(Me!Event_Name, "'", '"''") & "'"

this "function" of duplicating the single quotes in the strings is what I have put into a function in all my vba projects.
Avatar of sglee

ASKER

Thank you for your help!
Avatar of sglee

ASKER

When I used:
 Rst.FindFirst "[Event_Name] = '" & replace(Me!Event_Name, "'", '"''") & "'"

I got an error. I think quotes inside replace function is not right?
sorry, I had reversed some quotes ...
 Rst.FindFirst "[Event_Name] = '" & replace(Me!Event_Name, "'", '"''") & "'"
Avatar of sglee

ASKER

That caused an error too.
 replace(Me!Event_Name, "'", '"''")  ---> you are replace a single quote occurance (') with what?
1 single quote with 2 single quotes...
Avatar of sglee

ASKER

User generated image
I magnified the text from Replace function you provided above.
Can you correct this syntax and magnify it for me?
no, it must be there doublequote singlequote singlequote doublequote
and indeed, I had a leading single quote too much
that's what I tell the "quoting hell"
Avatar of sglee

ASKER

That worked. Thank you. Yes it is  "quoting hell"
doublequote singlequote singlequote doublequote was very effective.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial