Solved

LIKE in VBA

Posted on 2011-03-21
9
875 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 18

Expert Comment

by:Cluskitt
ID: 35182798
Try this:

If Not InStr(Ucase(g_strFullPathExcelFileKT),Ucase(g_strOrganizationShortName)) Then
0
 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

809 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