Solved

Error in FindFirst when the string has single quatation

Posted on 2014-01-23
12
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 143

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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 143

Expert Comment

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

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 143

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 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

726 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