Solved

Error in FindFirst when the string has single quatation

Posted on 2014-01-23
12
315 Views
Last Modified: 2014-01-23
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.
0
Comment
Question by:sglee
12 Comments
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 250 total points
ID: 39804651
You can do:

Rst.FindFirst "[Event_Name] = " & chr$(34) & Me!Event_Name & chr$(34)

Jim.
0
 

Author Comment

by:sglee
ID: 39804670
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?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39804705
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.
0
 

Author Comment

by:sglee
ID: 39804727
Thank you for your help!
0
 

Author Comment

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

I got an error. I think quotes inside replace function is not right?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39804750
sorry, I had reversed some quotes ...
 Rst.FindFirst "[Event_Name] = '" & replace(Me!Event_Name, "'", '"''") & "'"
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:sglee
ID: 39804838
That caused an error too.
 replace(Me!Event_Name, "'", '"''")  ---> you are replace a single quote occurance (') with what?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39804845
1 single quote with 2 single quotes...
0
 

Author Comment

by:sglee
ID: 39804877
Replace
I magnified the text from Replace function you provided above.
Can you correct this syntax and magnify it for me?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39804908
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"
0
 

Author Comment

by:sglee
ID: 39804928
That worked. Thank you. Yes it is  "quoting hell"
doublequote singlequote singlequote doublequote was very effective.
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 50 total points
ID: 39805113
I use a function.
Public Function fnWrap(WrapWhat as Variant, Optional WrapWith as String = """") as Variant

fnWrap = WrapWith & Replace(WrapWhat, WrapWith, WrapWith & WrapWith) & WrapWith

End Function

Open in new window

Then I just use:

Rst.FindFirst "[Event_Name] = " & fnWrap(Me!Event_Name)

By default this wraps the text in double quotes and replaces the double quotes embedded within the WrapWhat argument with two double quotes.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now