Using Like in an if statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CD Tom
    Contributor
    • Feb 2009
    • 495

    Using Like in an if statement

    I've used the if statement with the like many times and it works fine. My problem this time is I'm using a variable and it doesn't work. Here's the code
    Code:
        Dim v1 As String
        v1 = Forms("editmasterWannual").Controls("tabctl196").Pages(1).Caption
        V2 = Forms("editmasterWannual").Controls("tabctl196").Pages(2).Caption
        v3 = Forms("editmasterWannual").Controls("tabctl196").Pages(3).Caption
        v4 = Forms("editmasterWannual").Controls("tabctl196").Pages(4).Caption
        Dim testCheck As Boolean
        V1a = v1 + "*"
        testCheck = VBanqName Like V1a
        If testCheck = True Then
    the testCheck always shows False. I'm getting the name v1 from the controls in my form the v1 Variable shows "Warm Up Monday" I get the VBanqName from a different table and it shows "Warm Up Monday" it could also show "Warm Up Monday Early" that is why I want to add the "*" so it will pick up everything with "Warm Up Monday" in the VBanqName. I've tried different ways to the if statement to work like
    Code:
    If VBanqName Like V1a then
    but that doesn't work either. If I just type "Warm Up Monday*" then it works fine. Does anybody have any clues as to why the variable doesn't work.
    Thanks
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Turn your test around.
    Code:
    testCheck=V1a Like VBanqName
    It makes a difference.

    Jim

    Comment

    • CD Tom
      Contributor
      • Feb 2009
      • 495

      #3
      Tried that but still get the false answer. This is really getting annoying you would think the like statement would work with a variable. Got any other suggestions, thanks for your help.

      Comment

      • CD Tom
        Contributor
        • Feb 2009
        • 495

        #4
        Jim, sorry but that did work. First time I tried I typed something wrong. Thanks for your help.

        Comment

        • CD Tom
          Contributor
          • Feb 2009
          • 495

          #5
          Ok, that is working, now what I do is close that form. Now I'm back at the original form, but I want to set the focus on the tabctl that I found in the other form. Is that a possibility? An example would be in the other form I found the control in Pages(1) control when I come back to the original form I want the focus to be on that page. Does that make sense?

          Comment

          • jimatqsi
            Moderator Top Contributor
            • Oct 2006
            • 1293

            #6
            So glad your problem is solved. Sometimes it's just the simplest thing to change.d

            You'll have to start a new question. Multiple questions in one thread is not allowed.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32653

              #7
              If V1a = "Warm Up Monday*" then I fail to see how the proposed solution makes any sense. What am I missing here?
              Code:
              testCheck=V1a Like VBanqName
              ==>
              Code:
              testCheck="Warm Up Monday*" Like VBanqName
              This is simply wrong and shouldn't work under any circumstances I know.

              That said, while I'd always parenthesise assignments of boolean expressions, the original should work perfectly well assuming all other details have been related accurately.
              Code:
              testCheck = VBanqName Like V1a
              ==>
              Code:
              testCheck = (VBanqName Like "Warm Up Monday*")
              This should work when the pattern is matched.

              Comment

              • CD Tom
                Contributor
                • Feb 2009
                • 495

                #8
                The problem was there was a extra space between the warm up and the Match. I didn't notice because when looking at the variable it was hard to see the extra space. I sometimes need to take a closer look, I must have banged my head against the desk so many times wondering why that like statement didn't work. I do appreciate all the help and patience you guys have for some of my problems.
                Thanks for all your help.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32653

                  #9
                  Thanks for the update Tom.

                  I'm going to reset Jim's Best Answer (Sorry Jim). It is not good to direct other readers to something that's actually wrong. It's not intended as criticism in any way. It was helpful to trigger Tom to find his solution after all.

                  Comment

                  • jimatqsi
                    Moderator Top Contributor
                    • Oct 2006
                    • 1293

                    #10
                    I see the need to edit this post. I must be getting cross-eyed in my old age. The original post had the LIKE statement formatted correctly. So did my quote of the original post, LOL.

                    The example below may prove useful to some newbie, even though it is not relevant to this post. So I guess I'll leave that part alone.

                    LIKE does not work the same in both directions, as demonstrated here:
                    Code:
                    Private Sub Do_cmd_Click()
                    
                    Dim chk As Boolean
                    Dim A1 As String
                    Dim A2 As String
                    
                    A1 = "ABCXYZ"
                    A2 = "ABC*"
                    
                    chk = A1 Like A2
                    Debug.Print "A1 Like A2: " & chk
                    chk = A2 Like A1
                    Debug.Print "A2 Like A1: " & chk
                    
                    End Sub
                    The above code yields:
                    A1 Like A2: True
                    A2 Like A1: False

                    Jim
                    Last edited by jimatqsi; Jan 27 '16, 03:29 AM. Reason: incorrect posting

                    Comment

                    Working...