Solved

LIKE in VBA

Posted on 2011-03-21
9
874 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views 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 Access…
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…

810 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