Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error in FindFirst when the string has single quatation

Posted on 2014-01-23
12
Medium Priority
?
322 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 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 1000 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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 48

Assisted Solution

by:Dale Fye
Dale Fye earned 200 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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

705 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