Solved

LIKE in VBA

Posted on 2011-03-21
9
866 Views
Last Modified: 2013-11-27
I have some code in Access 2007 that tries to make sure a file selected by the user is appropriate for importing.  One of the issues is making sure a specific keyword is in the name of the file, surrounded by spaces.  

To do this I use the following statement:
    If Not g_strFullPathExcelFileKT Like "* " & g_strOrganizationShortName & " *" Then
        blah blah blah
        Exit Sub
    End If

Whenever there is an exact match, including case, the logic works, but when there is a discrepancy in case, it does not work.

As an example, assume g_strFullPathExcelFileKT  has a value of "C:My file for importing to Abcdef on 2011-03-21.xlms" and g_strOrganizationShortName  has a value of "Abcdef", a match is found.

But if the case is different, it does not work.  As an example, assume g_strFullPathExcelFileKT  has a value of "C:My file for importing to Abcdef on 2011-03-21.xlms" and g_strOrganizationShortName  has a value of "ABCDEF", a match is not found.

But, I do not care if the file selected matches on case or not as long as the character sequence is there.  How can I code my LIKE comparison so that a file name containing " Abcdef " or " abcdef " or " ABCDEF " will cause a match?
0
Comment
Question by:wsturdev
9 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35182701
In VBA, LIKE is used with InStr and InStrRev
0
 
LVL 1

Author Comment

by:wsturdev
ID: 35182738
I don't mean to be dense, but can you give me an example vis-a-vis my code?  How would I rewrite my code properly?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 35182767
How about:

If Not UCASE(g_strFullPathExcelFileKT) Like "* " & UCASE(g_strOrganizationShortName) & " *" Then
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35182798
Try this:

If Not InStr(Ucase(g_strFullPathExcelFileKT),Ucase(g_strOrganizationShortName)) Then
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Expert Comment

by:omgang
ID: 35182802
wsturdev, perhaps something else is causing a problem becuase this works for me in Access 2007.
OM Gang

Public Function TestLike()
On Error GoTo Err_TestLike

    Dim g_strFullPathExcelFileKT As String, g_strOrganizationShortName As String
   
    g_strFullPathExcelFileKT = "C:My file for importing to Abcdef on 2011-03-21.xlms"
    g_strOrganizationShortName = "abcdef"

    If Not g_strFullPathExcelFileKT Like "* " & g_strOrganizationShortName & " *" Then
        MsgBox "Not Like"
    Else
        MsgBox "Like"
    End If

Exit_TestLike:
    Exit Function

Err_TestLike:
    MsgBox Err.Number & ", " & Err.Description, , "Error in Function TestLike of Module Module8"
    Resume Exit_TestLike
End Function
0
 
LVL 11

Accepted Solution

by:
LambertHeenan earned 500 total points
ID: 35182853
Make sure the code module this runs in has

Option Compare Database

at the start of the module. That will ensure non-cases sensitive comparisons.

I would use Instr() for this task, as in

If Instr(g_strFullPathExcelFileKT," " & g_strOrganizationShortName & " " ) = 0 then ' NOT found in the file name
        blah blah blah
        Exit Sub
    End If

0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 35182870
Just a thought... if case doesn't matter, why not convert the case...

If Not lcase(g_strFullPathExcelFileKT) Like "* " & lcase(g_strOrganizationShortName) & " *" Then
0
 
LVL 1

Author Closing Comment

by:wsturdev
ID: 35183136
Option Compare Database did it.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35183197
If you do not have a module open in the database the default use of Option Compare is Binary in which different cases will not compare.  Your only options are to create a module and make the first line Option Comare Text or Option Compare Database - or use the lcase() or ucase() functions as leew suggested above.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

706 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

18 Experts available now in Live!

Get 1:1 Help Now