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

x
?
Solved

special characters in a string

Posted on 2000-03-13
8
Medium Priority
?
307 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 60 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Independent Software Vendors: 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 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