Solved

special characters in a string

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…
Suggested Courses
Course of the Month7 days, 6 hours left to enroll

622 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