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
Solved

special characters in a string

Posted on 2000-03-13
8
292 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro: print each sheet to pdf using ExportAsFixedFormat 11 52
VBS file using code from 2nd file (txt or vbs) 4 42
Add and format columns in vb6 7 63
Problem to line 23 53
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 While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

840 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