Solved

special characters in a string

Posted on 2000-03-13
8
285 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
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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

22 Experts available now in Live!

Get 1:1 Help Now