Solved

LIKE and WILDCARD problems with VBA and variables

Posted on 2004-08-27
11
611 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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
 

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

770 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