Solved

LIKE in VBA

Posted on 2011-03-21
9
879 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 (Access MVP)
ID: 35182767
How about:

If Not UCASE(g_strFullPathExcelFileKT) Like "* " & UCASE(g_strOrganizationShortName) & " *" Then
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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…
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.

717 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