Solved

LIKE and WILDCARD problems with VBA and variables

Posted on 2004-08-27
11
610 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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Deadlocks 12 50
MSSQL: Replace text (typo) 7 33
how to install/upgrade the Blitz responder kit 8 25
Help with Sorting Full Text results 2 14
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

895 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

11 Experts available now in Live!

Get 1:1 Help Now