Solved

special characters in a string

Posted on 2000-03-13
8
297 Views
Last Modified: 2010-05-02
This should be an extremely easy problem, but vb is not my primary language.

How do you pass special characters like single and double quotes in a string. Specifically this is in VBA and I am using the following function:

rst.FindFirst ("field = '" & strSearch & "')

Only strSearch could contain a single quote, which will cause an error in the function. It takes the single quote as the end of the string to be searched for.

Thankyou in advance for any help.
Chris
0
Comment
Question by:BurntBird
[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
8 Comments
 
LVL 14

Expert Comment

by:wsh2
ID: 2614776
Single quotes are NOT a control character in VB.. use it like any other character.. <smile>.

The double quote IS a control character.. If you want to place it in a string.. just use two of them together. For example, the string - This Is "My Book Title" - would look like

"This Is ""My Book Title"""

0
 
LVL 2

Expert Comment

by:gelog
ID: 2614816
rst.FindFirst "field=" & Chr(34) & strSearch & Chr(34)
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 20 total points
ID: 2614857
gelog:
You and I BOTH read the question wrong.. (uhhhh Thanks for locking the question down.. so no one else would catch it.. <sheesh>). Please let others comment before you lock it down.. <smile>.

Burntbird:
The part that we both missed is this..

"strSearch could contain a single quote, which will cause an error in the function"

and Burntbird is correct in this.

Fortunately, the solution is the same for Single Quotes as it is for Double Quotes.. <smile>. Prior to executing the strSearch command add the following statement before it..

strSearch = Replace (strSearch, "'", "''")

ie. one single quote in the Find parameter.. TWO single quotes in the Replace parameter.. each surrounded by double quotes.

0
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!

 

Author Comment

by:BurntBird
ID: 2617284
I'm sorry wsh2 is right. I should have made the problem more clear.  Also, I had already tried your solution and it did not work.
0
 

Author Comment

by:BurntBird
ID: 2617292
I am currently testing wsh2's answer, and will resond as soon as posible.

Thanks for the reponces.
0
 
LVL 2

Expert Comment

by:harnal
ID: 2629215
Actuall the chr(34) ASCII code would be the programatically correct way of creating a double quote string delimiter.  altho there are other mentioned methods that would also work fine.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2629274
Harnal is correct.. its a matter of style though. Using chr(34)s makes your statements longer and tends to make the reading of them less comprehensible. While on the other hand.. using the native quote chararacter can make the reader go blind. <sighing>.. its 6 of one, half a dozen of the other.. in either case.. neither is pretty.. and up to the developer to decide which suits their purpose best.. <smile>.
0
 

Author Comment

by:BurntBird
ID: 2640210
I finally tested this. Putting the second single quote into the string worked.  Unfortunately the replace() function didn't exist in VBA. At least it didn't in the libraries I had referenced. I now have a problem with MS Word not taking 'a/a' or 'a@a' as a whole word. But that is another problem, and I think for another group.

Thankyou for the help.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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