Results 1 to 6 of 6
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919

    InStr expression as Case in Select not as expected


    I have a new DB where a description field in one of its tables is cluttered with extraneous data that resulted from a migration process. In an attempt to clean things up a bit where there was an abundance of repetition, I created a function to be used in an UPDATE query to replace the description field with only the essential. I monitored the function in Debug but none of the InStr Case statements returned a value greater then zero, whereas an isolated debug statement (not shown here) clearly indicated that the InStr expressions were in fact finding the desired strings as coded.

    Is there some special syntax consideration when the InStr function is used to test a "Case"?

    Code:
    Option Compare Database
    Option Explicit
    
    
    Public Function FixDesc(strDes As String) As String
    
    
    FixDesc = strDes
    
    
        Select Case FixDesc
            Case InStr(strDes, "Amazon") > 0
                FixDesc = "Amazon"
            Case InStr(strDes, "AMZN") > 0
                FixDesc = "Amazon"
            Case InStr(strDes, "APPLE.COM/BILL") > 0
                FixDesc = "APPLE.COM/BILL"
            Case InStr(strDes, "HUTCHS MISSION") > 0
                FixDesc = "HUTCHS MISSION"
            Case InStr(strDes, "IN TOUCH") > 0
                FixDesc = "IN TOUCH"
            Case InStr(strDes, "LA CUCINA") > 0
                FixDesc = "LA CUCINA"
            Case InStr(strDes, "METAMORPHOSIS") > 0
                FixDesc = "METAMORPHOSIS"
            Case InStr(strDes, "Netflix") > 0
                FixDesc = "Netflix"
            Case InStr(strDes, "PACIFIC JUSTICE") > 0
                FixDesc = "PACIFIC JUSTICE"
            Case InStr(strDes, "QVC") > 0
                FixDesc = "QVC"
            Case InStr(strDes, "SAVEMART") > 0
                FixDesc = "SAVEMART"
            Case InStr(strDes, "SWANSON") > 0
                FixDesc = "SWANSON"
            Case InStr(strDes, "VILLAGE MISSIONS") > 0
                FixDesc = "VILLAGE MISSIONS"
        End Select
        
    End Function

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Do you have some sample records showing the sort of things in strDes?

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Your Select Case doesn't make sense unless FixDesc has value of True or False because each of those Case InStr() > 0 expressions will return either True or False. You are looking for substring within strDes so strDes certainly not a True or False. Then you set FixDesc equal to strDes which is not True or False.

    So try:

    Select Case True
    ...
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    Sure, "SHP*SWANSON HLTH PROD 800-437-4148 ND 3664 7294 685603425201*5OF5"

    I expected the statement "Case InStr(strDes, "SWANSON") > 0" to return a value of 5

  5. #5
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    The case expression doesn't work like that

    You are Testing the result of FixDesc which will never equal any of the subsequent case statements
    As June suggested you need to use something like

    Code:
    SELECT CASE True
         CASE ....
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    1,919
    PERFECT!

    I was, perhaps not obviously, thinking "In the case where InStr(strDes, "SWANSON") > 0" set the return value, but ignoring the object of the Select.

    Thanks,
    Bill

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Case statement not working as Expected
    By Dave14867 in forum Access
    Replies: 5
    Last Post: 08-07-2020, 09:14 AM
  2. Select Case without case
    By DMT Dave in forum Access
    Replies: 4
    Last Post: 11-13-2019, 08:21 PM
  3. InStr not as expected
    By GraeagleBill in forum Programming
    Replies: 3
    Last Post: 08-10-2018, 05:53 PM
  4. Replies: 2
    Last Post: 04-07-2016, 07:19 AM
  5. Pl sql case select
    By mrmmickle1 in forum Queries
    Replies: 1
    Last Post: 11-17-2015, 11:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums