UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Using Like * With An Apostrophe In The User Name, Access 2007    
 
   
Bob G
post Jan 24 2013, 03:55 PM
Post #1

UtterAccess VIP
Posts: 8,191
From: CT



i have this piece of code that works fine, EXCEPT if the name has an apostrophe in it? I can't change the way the users spell their name so I need to modify this somehow

CODE
strstring = "[mytable].FullName like '" & "*" & LBFullname & "*" & "'"
Go to the top of the page
 
+
BananaRepublic
post Jan 24 2013, 04:03 PM
Post #2

Rent-an-Admin
Posts: 8,925
From: Banana Republic



Double up the delimiter.

CODE
... & Replace(LBFullname, "'", "''") & ...


I usually write a SanitizeString function which also strips out other metacharacters. Be afraid of Bobby Table. (IMG:style_emoticons/default/wink.gif)
Go to the top of the page
 
+
Bob G
post Jan 24 2013, 04:22 PM
Post #3

UtterAccess VIP
Posts: 8,191
From: CT



i cant strip out the apostophe. i need to be able to match on the name with it.

so, if it is bobby o'VIP then i have to find bobby o'VIP
Go to the top of the page
 
+
BananaRepublic
post Jan 24 2013, 04:24 PM
Post #4

Rent-an-Admin
Posts: 8,925
From: Banana Republic



Bob, I didn't strip it out in the provided code.
Go to the top of the page
 
+
Bob G
post Jan 24 2013, 04:26 PM
Post #5

UtterAccess VIP
Posts: 8,191
From: CT



color me confused. am i supposed to use the replace in with what i provided or is it a sample of what i need to do
Go to the top of the page
 
+
Bob G
post Jan 24 2013, 04:32 PM
Post #6

UtterAccess VIP
Posts: 8,191
From: CT



Thanks BR !!

I got my other brain cell to participate and got it working with your addition.

would never have thought to use a replace like that
Go to the top of the page
 
+
BananaRepublic
post Jan 24 2013, 04:36 PM
Post #7

Rent-an-Admin
Posts: 8,925
From: Banana Republic



In the sample I provided:

CODE
... & Replace(LBFullname, "'", "''") & ...


what it means is that "bobby o'VIP" will be then changed to "bobby o''VIP" (note the doubled up '')

and the SQL becomes:

CODE
...WHERE FullName LIKE '*bobby o''VIP*'...


which cause SQL to search for anything that contains "bobby o'VIP" (note single quote".

By doubling up the delimiter, you "escape" it so that SQL will treat the doubled up '' as a literal ' and not as a delimiting ' signaling end of string.

I then mentioned that generally you want to strip out other metacharacters like "*" or "%" from the search or at least make them literal by wrapping them in a bracket. Thus to find a record containing "A * B", the expression need to be :

CODE
ColumnName LIKE "*A [*] B*"

(untested aircode)

which will match only phrases "A * B" but not "A B" or "A C B".

Helps?
Go to the top of the page
 
+
Bob G
post Jan 24 2013, 04:43 PM
Post #8

UtterAccess VIP
Posts: 8,191
From: CT



helpful as always.

just reminds me of how much i really don't know.

but, thanks again, it worked exactly as I needed it.

Had a listbox of resources and they were told to double click on their name to see their respective projects. It didn't work for the one person who had an apostrophe in their name.

Now, thanks to you, the next version of the accde will have THAT fixed.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 18th June 2013 - 08:54 PM