?
Solved

LIKE in VBA

Posted on 2011-03-21
9
Medium Priority
?
881 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 48

Expert Comment

by:Dale Fye
ID: 35182767
How about:

If Not UCASE(g_strFullPathExcelFileKT) Like "* " & UCASE(g_strOrganizationShortName) & " *" Then
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 2000 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 96

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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Suggested Courses

800 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