Results 1 to 9 of 9
  1. #1
    kdleaver1974 is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Dec 2024
    Posts
    13

    Need a simpler way to program with IF/THEN Logic


    First, let me thank everyone with their help with various issues of my VBA Program.

    Second, this isn't a problem more an inquiry of how to make things "cleaner" in writing my code.

    What I am trying to do, using IF/THEN, is determine if a variable does NOT have a certain value, and if it doesn't, then to find the condition true.

    The variable is a SKU that has a "-" in it in either the 5th, 6th, and 7th Character. I want to search the SKU and if it does NOT find the "-" in the SKU then to have the IF/THEN condition be true.

    The only way I seem to be able to do this is the following way:

    Code:
            If Mid(rcdFindItemSKU, 6, 1) = "-" Or _
             Mid(rcdFindItemSKU, 7, 1) = "-" Or _
             Mid(rcdFindItemSKU, 8, 1) = "-" Then
             
             Else
             
                AddLetterTorcdFindItemSKU = ""
                KeyCode = vbKeyShift
                            
                Exit Sub
                
            End If
    The original code was the following:

    Code:
            If Mid(rcdFindItemSKU, 6, 1) <> "-" Or _
             Mid(rcdFindItemSKU, 7, 1) <> "-" Or _
             Mid(rcdFindItemSKU, 8, 1) <> "-" Then
             
                AddLetterTorcdFindItemSKU = ""
                KeyCode = vbKeyShift
                            
                Exit Sub
                
            End If
    The problem with the above code is two of the conditions will always be TRUE because if the Dash is in character 6, then characters 7 and 8 will be TRUE.

    Is there another way to write the logic so I don't to use the Else statement? Thanks.

    Ken L

  2. #2
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    If you just need to determine if the string has a dash in it, use the instr function. Below is true if no dash is present.

    If InStr(rcdFindItemSku, "-") = 0 Then

  3. #3
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    The original code is wrong because of the OR's; that must be AND's.
    Groeten,

    Peter

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    You say 5th,6th or 7th character but are using 6, 7 or 8?
    Code:
    tt="xxxx---xxxxx"
    ? mid(tt,5,1)
    -
    ? mid(tt,6,1)
    -
    ? mid(tt,7,1)
    -
    I read your request as correctly having an OR, as the - could be in any of those 3 positions.?

    I was thinking of a regex expression which are quite powerful.
    As you would appear to need this quite often, that is the way I would go.

    Here is what ChatGPT offered. I tested it and it works.
    Code:
    Function HyphenInPosition(ByVal txt As String) As Boolean
        Dim regex As Object
        Set regex = CreateObject("VBScript.RegExp")
        
        With regex
            .Pattern = "^.{4,6}-"
            .IgnoreCase = True
            .Global = False
        End With
        
        HyphenInPosition = regex.TEST(txt)
    End Function
    Sub TestRegex()
        Debug.Print HyphenInPosition("1234-abc")    ' True (hyphen at 5th char)
        Debug.Print HyphenInPosition("12345-abc")   ' True (hyphen at 6th char)
        Debug.Print HyphenInPosition("123456-abc")  ' True (hyphen at 7th char)
        Debug.Print HyphenInPosition("1234---56abc") ' True at them all
        Debug.Print HyphenInPosition("123456abc")
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    I would go for

    if not (Mid(rcdFindItemSKU, 6, 3) like “*-*”) then
    AddLetterTorcdFindItemSKU = ""
    KeyCode = vbKeyShift

    Exit Sub

    End If

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    I think we need clarification as to what the SKU can possibly be?
    As #5 would not process "xxxx5-7xxxxx" correctly.

    My interpretation was a - in any of the mentioned postions?

    The variable is a SKU that has a "-" in it in either the 5th, 6th, and 7th Character.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    OP says they want to simplify the code, not that the code doesn’t work- so I’m simplifying the logic provided rather that interpreting the description

    As #5 would not process "xxxx5-7xxxxx" correctly.
    ?not (Mid("xxxx5-7xxxxx", 6, 3) like "*-*")
    False

    works for me

    as does this if the its chars 5-7 rather than 6 to 8

    ?not (Mid("xxxx5-7xxxxx", 5, 3) like "*-*")
    False

  8. #8
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Simplified for readability -

    Code:
    Function MySku(strIN As String, strMatch As String) As Boolean
    
        Select Case InStr(1, strIN, strMatch)
    
            Case 5, 6, 7
                MySku = False
                
            Case Else
                MySku = True
                
        End Select
    
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    jojowhite's Avatar
    jojowhite is online now Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    433
    you may also try:
    Code:
    If Instr(Mid$(rcdFindItemSKU, 5, 3), "-") <> 0 Then
    ' dash is in the sKu
    Else
    ' dash not found
    End If

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

Similar Threads

  1. Replies: 2
    Last Post: 07-23-2023, 10:29 PM
  2. Simpler Code
    By Thompyt in forum Programming
    Replies: 1
    Last Post: 02-08-2023, 02:36 PM
  3. Replies: 3
    Last Post: 07-27-2016, 03:02 AM
  4. Replies: 4
    Last Post: 06-24-2016, 04:03 PM
  5. Which Export to Excel Method is Simpler to Implement?
    By Heatshiver in forum Import/Export Data
    Replies: 16
    Last Post: 03-26-2012, 07:01 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