Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dlookup On Stings Containing An Apostrophe Or Quote 2

Status
Not open for further replies.

DevelopV

Technical User
Mar 16, 2012
113
ZA
Dlookup on strings that contain an apostrophe (') or a quote (") results in an error!

eg
strCtriteria = "Apple"
ProductId = dlookup("ProductId","tblProduct","Product = '" & strCtriteria & "'") works

but if
strCtriteria = "Apple's"
ProductId = dlookup("ProductId","tblProduct","Product = '" & strCtriteria & "'") fails - syntax error (missing operator) in query expression......

Is it only apostrophe (') and quote (") that I need worry about?

I never know the position of the apostrophe (') or quote (") in the string!

How do I handle strings that may contain an apostrophe (') or a quote (")?

Thanks in advance
 
How are ya DevelopV . . .

Try:
Code:
[blue]strCtriteria = Replace("Apple's", "'", "''")[/blue]
[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Do I only have to worry about apostrophe?

Do I have to worry about quotes?
 
DevelopV . . .

I've never seen or heard of a quotation being used in a name. However ... incase you do have this problem the following should do:
Code:
[blue]strCtriteria = Replace(Replace("Apple's", "'", "''"), """", "''")[/blue]
You have this problem because [blue]apostrophe & quote[/blue] are control characters in access that are used in pairs. Its the oddball that confuses parsing and causes errors. When you want to include them in text you need to use two ... (Apple[red]''[/red]s instead of Apple[red]'[/red]s) or (Apple[red]""[/red]s instead of Apple[red]"[/red]s). This is what the dual [blue]Replace[/blue] function does. It replaces the apostrophe or quote with two apostrophe's. Note that the source of the value doesn't change ... its the strCriteria that does ... and this is proper.

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
My problem is that users can include both apostrophe & quote in a text field. They may want to include these in, for example, a product description. In certain instances I need to do a dlookup using the field.
 
A safe way:
Code:
ProductId = DLookup("ProductId", "tblProduct", "Product='" & Replace(strCtriteria, "'", "''") & "'")
Don't worry for double quote with the above

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
DevelopV . . .

The function takes care of both. Just have modify slightly to take care of double quotations. Try the following:
Code:
[blue]strCriteria = Replace(Replace(strCriteria, "'", "''"), """", """""")[/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top