dlookup to handle both " and '

  • Thread starter Thread starter Andrew Brill
  • Start date Start date
A

Andrew Brill

say I have a dlookup as follows (I've put incorrect spaces around the quotes
so you can read it!):

dlookup("whatever","whereever","field = ' " & userinput & " ' ")
this works if the user enters " in their input but not '

now I can change this to
dlookup("whatever","whereever","field = "" " & userinput & " "" ")
and the user can then enter ' but not "

so... how do I write it so they can enter either?

Thanks,
Andrew
 
hi Andrew,

Andrew said:
dlookup("whatever","whereever","field = ' " & userinput & " ' ")
this works if the user enters " in their input but not '
now I can change this to
dlookup("whatever","whereever","field = "" " & userinput & " "" ")
and the user can then enter ' but not "
so... how do I write it so they can enter either?
Escape always the single quote:

DLookup("", "", "field='" & Replace(input, "'", "''") & "'")

Replace is available >= A2K.

mfG
--> stefan <--
 
You're going to have to use the Replace function on their input to change
their input to either have two single quotes in a row, or two double quotes
in a row, depending on what you're using as a delimiter (if you're
delimiting with a single quote, you need to double up any of the single
quotes they input: if you're delimiting with a double quote, you need to
double up any of the double quotes they input):

DLookup("whatever","whereever","field = '" & Replace(userinput, "'", "''") &
"'")

or

DLookup("whatever","whereever","field = """ & Replace(userinput, """",
"""""") & """")

Exagerated for clarity, those are:

DLookup("whatever","whereever","field = ' " & Replace(userinput, " ' ", " '
' ") & " ' ")

or

DLookup("whatever","whereever","field = " " " & Replace(userinput, " " " ",
" " " " " ") & " " " ")

These could also be rewritten as:

DLookup("whatever","whereever","field = " & Chr$(39) & Replace(userinput,
Chr$(39), Chr$(39 & Chr$(39)) & Chr$(39))

or

DLookup("whatever","whereever","field = " & Chr$(34) & Replace(userinput,
Chr$(34), Chr$(34 & Chr$(34)) & Chr$(34))


For more information, check my May, 2004 "Access Answers" column in Pinnacle
Publication's "Smart Access". You can download the column (and sample
database) for free at http://www.accessmvp.com/djsteele/SmartAccess.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Using Max or DMax in DLookUp criteria? 1
Refresh Dlookup Values on Several Tabs 1
dlookup and string for criteria 5
DLookUp syntax 9
DLookup 5
DLookup Data Mismatch 1
DLookUp 1
Yet another DLOOKUP question 2

Back
Top