PDA

View Full Version : LIKE Function in VBA for Access???



sladerwilson
11-09-2007, 12:37 PM
I am a VBA newbie... I have an ASP Script that I wrote that contains the following SQL statement:

objRst.Open "UPDATE export SET Field48 = 'C' WHERE Field2 LIKE 'ST-%'"

I've written it in VBA as:

sql1 = "UPDATE export SET Field48 = 'C' WHERE Field2 LIKE 'ST-%'"
DoCmd.RunSQL sql1

When I run this VBA in Access it doesn't find/update any records that contain ST-xxxxx although this works flawlessly in ASP.

I am guessing VBA doesn't recognize the % or is it something else altogether?

Any help would be greatly appreciated.

sladerwilson
11-09-2007, 12:52 PM
Sorry... I got it figured out. Just in case someone else runs into this....

Bad Code:
sql1 = "UPDATE export SET Field48 = 'C' WHERE Field2 LIKE 'ST-%'"
DoCmd.RunSQL sql1

Good Code:
sql1 = "UPDATE export SET Field48 = 'C' WHERE Field2 LIKE 'ST-*'"
DoCmd.RunSQL sql1

Just replaced the % with *

akn112
11-14-2007, 07:30 AM
depending on where you are running the code from can determine the syntax.

"If you use wildcard characters in your SQL queries, you may have found that queries that used to work just fine in Access 97 suddenly cause problems when using Access 2000 and ADO. That's because DAO and ADO use different wildcard characters. In DAO, you use a question mark (?) as a wildcard for any individual character and the asterisk (*) as a wildcard for any string. However, using ADO, you must use the underscore character (_) to match a single character and the percent symbol (%) to match a string. "

source: http://www.klippert.com/tcc/Access/inewsnetau.htm#Use%20the%20right%20wildcard%20with%20SQL%20queries%20(Acces s%2097/2000)