Link to home
Start Free TrialLog in
Avatar of Steffee
Steffee

asked on

LIKE and WILDCARD problems with VBA and variables

I am using Access front end on SQL Server backend. I am trying to get a SQL query in VBA code to execute correctly. I am having problems with the LIKE operand. Here is the query - is my syntax incorrect? The field jbISBN is a varchar field, the variable I am using to match is a string.

DoCmd.RunSQL "update tblJob set jbCurrentlyBilling = 1 from dbo.tblJob  " & _
       " where tblJob.jbBillTag = 1 and tblJob.jbISBN like " * " & '" & strIssueSearch & "' & " * ""

I have tried using different wildcard symbols and preconcatenating the string with the asterix, but am having no luck whatsoever. Any suggestions greatly appreciated as Im going mad trying to get this to work...

Thanks

Steph
Avatar of ram2098
ram2098

DoCmd.RunSQL "update tblJob set jbCurrentlyBilling = 1 from dbo.tblJob  " & _
       " where tblJob.jbBillTag = 1 and tblJob.jbISBN like " % " & '" & strIssueSearch & "' & " % ""

'%' is the wild card symbol that you have to use with like in SQL.
Hi Steph.

Access uses * and ? for wildcards, but ms-sql and most other sql databases use % and _ (underscore).

Best regards,
bragn.
Avatar of Steffee

ASKER

if I use % as in ram2098 suggestion I get an invalid character error, so I think I have to use the *, still struggling to get this to work...
ASKER CERTIFIED SOLUTION
Avatar of ram2098
ram2098

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steffee

ASKER

I am no longer getting invalid character errors but it is not perfornming the update requested i.e. setting jbCurrentlyBilling = 1.
The other where criteria are fulfilled, when I run this as a stored procedure the update occurs (I replace the variable name with the actual text: 06311 for example.Is it because it is a number I am trying to match? - although I am reading it in as a string as sometimes this contains letters as well.

Thanks

Steph
Steffee

Are you running this on a linked table or as a passthru query?
If this is a linked table in Access, you should use * and ?.  

Best regards,
bragn
It doesn't matter..whether it is a character (or) a number....

If it is runnng thru stored procedure means, I guess the value that you are passing from the Access may not be correct ..
Could you please verify whether "strIssueSearch" is getting the correct value as you are expecting?
Avatar of Steffee

ASKER

Thanks for your replies, I am running it on a linked table in Access, and yes the strIssueSearch variable is holding the correct value. The code is not erroring on the sql command it carries on through as if it has worked properly but does not perform the update.

Thanks for your help on this so far.

Steph
Avatar of Steffee

ASKER

Hi, well after a fresh look at it this morning, code in the on_change() event was counter acting the code in the after update event of my button. So in fact the syntax of ram2098's answer was correct.

Thank you!