Solved

LIKE and WILDCARD problems with VBA and variables

Posted on 2004-08-27
11
609 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Steffee
  • 5
  • 4
  • 2
11 Comments
 
LVL 11

Expert Comment

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

0
 
LVL 11

Expert Comment

by:ram2098
ID: 11911604
'%' is the wild card symbol that you have to use with like in SQL.
0
 
LVL 4

Expert Comment

by:bragn
ID: 11911644
Hi Steph.

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

Best regards,
bragn.
0
 

Author Comment

by:Steffee
ID: 11911666
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...
0
 
LVL 11

Accepted Solution

by:
ram2098 earned 500 total points
ID: 11911671
I guess there is small problem with quotes in your query...

DoCmd.RunSQL "update tblJob set jbCurrentlyBilling = 1 from dbo.tblJob  " & _
       " where tblJob.jbBillTag = 1 and tblJob.jbISBN like '%" & strIssueSearch & "%' "
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:Steffee
ID: 11911693
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
0
 
LVL 4

Expert Comment

by:bragn
ID: 11911712
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
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11911797
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 ..
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11911823
Could you please verify whether "strIssueSearch" is getting the correct value as you are expecting?
0
 

Author Comment

by:Steffee
ID: 11939714
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
0
 

Author Comment

by:Steffee
ID: 11939875
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!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now