Results 1 to 13 of 13

Thread: Multi condition in VB?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Multi condition in VB?

    Hello,

    I am new here and this is my first subject. I tried to find the solution to my problem in old topics and tutorial. I dont know how I can write my problem and maybe It's not possible in VB6.

    Actually im here
    Code:
    Sub CopyRimColor
    
        Dim screenText As String
        With Session
            .SetSelectionStartPos 11, 2
            .ExtendSelectionRect 20, 55
            .CopySelection
            screenText = .GetClipboardText
        End With
        
        
     'If screenText.Contains("ALU'') Then
     ' Session.SetClipboardText ("ALUM)
      '  Else: Session.SetClipboardText ("STL'')
    
     ' End If
      
    End Sub
    What i want to do it's somehing like that. Can you tell me how i can write it plz?

    Name:  Color rim.jpg
Views: 776
Size:  15.4 KB


    Any help will be welcome, Thank you!

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,140

    Re: Multi condition in VB?

    Example code with multiple potential conditions to check in each if statement, nested if statements, and 4 final branches (which I've numbered 1 through 4 from left to right) like you have in your diagram:

    Code:
    If a = 1 Or a = 2 Then
      If b = 1 Or b = 2 Then
        'Final branch 1 code goes here
      Else
        'Final branch 2 code goes here
      End If
    Else
      If b = 1 Or b = 2 Then
        'Final branch 3 code goes here
      Else
        'Final branch 4 code goes here
      End If
    End If
    Good luck.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: Multi condition in VB?

    I dont know if that can work like that… here an example of the info inside screentext
    It's not just a caracter... it's a chain of character where i want to search for specific term(STL, AL, ACC)

    FRONT TIRES : FR GY ENDURANCE LHS 11R22.5 14PR
    FRONT WHEELS: FR WHL ACCUR 50344 22.5X8.25 STL HVY
    GAWR : 12000.00 LB W/ TIRE SIZE:
    RIMS SIZE: : AT PSI COLD:
    1ST INTERM. GAWR : 0.00 LB W/ TIRE SIZE:
    RIMS SIZE: : AT PSI COLD:
    2ND INTERM. GAWR : 0.00 LB W/ TIRE SIZE:
    RIMS SIZE: : AT PSI COLD:
    REAR TIRES : RR GY G182 RSD 11R22.5 14PR AP HWY DR
    REAR WHEELS: RR WHL ACCUR 50344 22.5X8.25 STLHVYDUTY
    If i wrote
    If a = AL Or a = ALU Then
    If b = STL Or b = ACC Then
    'Final branch 1 code goes here
    It will never work… no?

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: Multi condition in VB?

    I dont know if that can work like that… here an example of the info inside screentext
    It's not just a caracter... it's a chain of character where i want to search for specific term(STL, AL, ACC)

    FRONT TIRES : FR GY ENDURANCE LHS 11R22.5 14PR
    FRONT WHEELS: FR WHL ACCUR 50344 22.5X8.25 STL HVY
    GAWR : 12000.00 LB W/ TIRE SIZE:
    RIMS SIZE: : AT PSI COLD:
    1ST INTERM. GAWR : 0.00 LB W/ TIRE SIZE:
    RIMS SIZE: : AT PSI COLD:
    2ND INTERM. GAWR : 0.00 LB W/ TIRE SIZE:
    RIMS SIZE: : AT PSI COLD:
    REAR TIRES : RR GY G182 RSD 11R22.5 14PR AP HWY DR
    REAR WHEELS: RR WHL ACCUR 50344 22.5X8.25 STLHVYDUTY
    If i wrote
    Code:
    If a = AL Or a = ALU Then
    If b = STL Or b = ACC Then
    'Final branch 1 code goes here
    It will never work… no?

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Multi condition in VB?

    Your string comparison looks a bit like VB.Net.
    Assuming you are using VB6, you would want to use Instr or possibly a regular expression, although I'm not competent in using regular expressions.
    Code:
    If (Instr(screenText, "ALU") >= 0) Or (Instr(screenText, "ACC") >= 0) Then
      '...
    p.s. the ">=" should just be ">" as VB6 substring indexes start at 1. Zero is returned when the substring is not found.
    Last edited by passel; Jun 13th, 2019 at 09:37 PM.

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,140

    Re: Multi condition in VB?

    The code I wrote is not intended to copy and paste verbatim. You asked about setting up multiple conditions and multiple branches. My code mimics your flowchart in terms of logical flow only, not in terms of evaluating the values you specified. It is up to you to determine how to properly write the conditions in the If statements to satisfy your needs.

    To answer your question, of course that branch can be reached. If at least one of the conditions in the first If statement is True, and then one of the conditions in the second If statement is true.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Multi condition in VB?

    A quick test
    Code:
    Private Sub Command1_Click()
      Dim s As String
      s = "Some random string with ALU in it"
      If InStr(s, "ALU") > 0 Then
        MsgBox "Its there"
      End If
    End Sub
    The test ended up showing that you just need to check for > 0, since the string index is 1 based. Checking for >= 0 is wrong.
    0 is what is returned if the substring isn't found.

    If you changed "ALU" in the string s, to "Alu", then you wouldn't get the message box, because the default comparsions is case sensitive.
    You can provide an extended version for case insensitive search.
    Code:
      If InStr(1, s, "ALU", vbTextCompare) > 0 Then

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: Multi condition in VB?

    Thank you for your code that help me… It gave me nice way to start

    Sub CopyRimColor()

    Dim screenText As String
    With Session
    .SetSelectionStartPos 11, 2
    .ExtendSelectionRect 20, 55
    .CopySelection
    screenText = .GetClipboardText
    End With

    'screenText = "Some random string with ALU in it"
    If InStr(screenText, "ALU") > 0 Then
    MsgBox "Its there"
    End If
    End Sub
    I will try to add an other If line slowly.

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,940

    Re: Multi condition in VB?

    To my eyes, this looks like a very specific example within a much larger project.

    If that's the case, you will obviously have some kind of database of all these keywords, as well as a defined hierarchy for them all. I've got no idea how that'll be organized, but that's certainly the first task: Define that organization.

    Then, I'll guess that there may be an indefinite number of levels to the hierarchy (depending on what the first/root word is (or words are)).

    So, with the extremely limited knowledge you've given us, here's what it looks like to me:

    • Possibly a procedure with a Param Array as an argument. The level's list of words (like, "ALU" or "AL") will be in the param array.
    • The function will fetch the words for the next level down in the hierarchy.
    • The function will also be recursive (called from itself). This will allow the number of levels in the hierarchy to be indefinite.

    Just some of my thoughts. And, this is not a trivial project.

    Good Luck,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: Multi condition in VB?

    You're right, it's a bigger project.

    But I only need this small part mentioned above. Because the big project is reserved 2 years to our team of '' professionals '' but I still wait and I lose an incredible time to check which of my 4 variable is present 500 times a week. I think that with a little help I should be able to write few line of code that should save me valuable time.
    Thank you

  11. #11

    Thread Starter
    New Member
    Join Date
    Jun 2019
    Posts
    6

    Re: Multi condition in VB?

    Here what is i did and it's working. Do you know if i can optimized it?

    [/Sub CopyRimColor()
    Dim screenText As String

    With Session
    .SetSelectionStartPos 11, 2
    .ExtendSelectionRect 20, 55
    .CopySelection
    screenText = .GetClipboardText

    End With

    'screenText = "Some random string with ALU in it"
    If (InStr(screenText, "AL") > 0) And (InStr(screenText, "ACC") > 0) Then
    'MsgBox "ALUM/STL"
    Wait (2)
    Session.SetClipboardText ("ALUM/STL")

    Else
    If (InStr(screenText, "AL") > 0) And (InStr(screenText, "STL") > 0) Then
    'MsgBox "ALUM/STL"
    Wait (2)
    Session.SetClipboardText ("ALUM/STL")

    Else
    If InStr(screenText, "AL") > 0 Then
    'MsgBox "ALUM"
    Wait (2)
    Session.SetClipboardText ("ALUM")

    Else
    If (InStr(screenText, "STL") > 0) Or (InStr(screenText, "ACC") > 0) Then
    'MsgBox "STL"
    Wait (2)
    Session.SetClipboardText ("STL")

    Else
    If (InStr(screenText, "STL") < 1) Or (InStr(screenText, "AL") < 1) Then
    MsgBox "rien"
    'Wait (2)
    Session.SetClipboardText (" ")

    End If

    End If

    End If

    End If

    End If

    End Sub

    And i tried to do a line like this but it did'nt work and i don't know if there is a way to do it.

    If (InStr(screenText, "AL") > 0) And ((InStr(screenText, "ACC") or (InStr(screenText, "STL")) > 0) Then
    ...


    AND Thank you Passel

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Multi condition in VB?

    Code:
    Dim lFlags As Long
    If InStr(screenText, "AL") > 0 Then lFlags = 1
    If InStr(screenText, "ACC") > 0 Then lFlags = lFlags Or 2
    If InStr(screenText, "STL") > 0 Then lFlags = lFlags Or 4
    Select Case lFlags
        Case 1 ' just has AL
            MsgBox "ALUM"
        Case 3, 5, 7 ' has AL+ACC(1 Or 2), AL+STL(1 Or 4), AL+ACC+STL(1 Or 2 Or 4)
            MsgBox "ALUM/STL"
        Case 2, 4, 6 ' has ACC(2), STL(4), or ACC+STL(2 Or 4)
            MsgBox "STL"
        Case Else ' has none of the above
            MsgBox "rien"
    End Select
    Or another way of using the bit flags:
    Code:
    Dim lFlags As Long
    If InStr(screenText, "AL") > 0 Then lFlags = 1
    If InStr(screenText, "ACC") > 0 Then lFlags = lFlags Or 2
    If InStr(screenText, "STL") > 0 Then lFlags = lFlags Or 4
    If (lFlags And 1) Then ' has AL
        If (lFlags And 6) Then ' has ACC and/or STL
            MsgBox "ALUM/STL"
        Else
            MsgBox "ALUM"
        End If
    ElseIf (lFlags And 6) Then ' has ACC and/or STL
        MsgBox "STL"
    Else ' does not have AL, STL, nor ACC
        MsgBox "rien"
    End If
    Using a bit Flag, as Long, can store up to 32 items (32 bits per long). Depending on how many you need, this may not be ideal.

    Edited: Disadvantage with the top example is that you must handle every possible combination that applies. Advantage with the bottom example is that you only need to test the parts you need. To try to make that clearer.
    If just wanting to know if ACC was in screenText, whether or not other text exists....
    In the top example, you would test for: Case 2, 3, 6, 7
    In the bottom example, you only test for: (lFlags And 2)
    Last edited by LaVolpe; Jun 15th, 2019 at 11:50 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Multi condition in VB?

    @cosmicmurder

    Enclose the code between [code] tag not [quote] and also indent it to easily follow nested statements

    Code:
    Sub CopyRimColor()
        Dim screenText As String
        
        With Session
            .SetSelectionStartPos 11, 2
            .ExtendSelectionRect 20, 55
            .CopySelection
            screenText = .GetClipboardText
            
        End With
        
        'screenText = "Some random string with ALU in it"
        If (InStr(screenText, "AL") > 0) And (InStr(screenText, "ACC") > 0) Then
            'MsgBox "ALUM/STL"
            Wait (2)
            Session.SetClipboardText ("ALUM/STL")
            
        Else
            If (InStr(screenText, "AL") > 0) And (InStr(screenText, "STL") > 0) Then
                'MsgBox "ALUM/STL"
                Wait (2)
                Session.SetClipboardText ("ALUM/STL")
                
            Else
                If InStr(screenText, "AL") > 0 Then
                    'MsgBox "ALUM"
                    Wait (2)
                    Session.SetClipboardText ("ALUM")
                    
                Else
                    If (InStr(screenText, "STL") > 0) Or (InStr(screenText, "ACC") > 0) Then
                        'MsgBox "STL"
                        Wait (2)
                        Session.SetClipboardText ("STL")
                        
                    Else
                        If (InStr(screenText, "STL") < 1) Or (InStr(screenText, "AL") < 1) Then
                            MsgBox "rien"
                            'Wait (2)
                            Session.SetClipboardText (" ")
                            
                        End If
                        
                    End If
                    
                End If
                
            End If
            
        End If
    
    End Sub



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width