Results 1 to 7 of 7

Thread: Error in IF statement

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    28

    Error in IF statement

    Hi All, I am using the below if statement that seems to be giving me problems. The first part of my statement is false, but I am getting a message box popup. I am not sure why I am getting the message if right away the beginning is false. The "OrElse" is true, but thats it.

    Code:
    If (CmbLateStart.Visible = True AndAlso CmbLateStart.SelectedItem = "---" OrElse CmbLateStart.Text = vbNullString) Then

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,332

    Re: Error in IF statement

    Hmmm, I am not sure but I would ommit the " = True" part seeing as it is unnecessary and move the brackets:

    Code:
    If (CmbLateStart.Visible AndAlso CmbLateStart.SelectedItem) = "---" OrElse CmbLateStart.Text = vbNullString Then
    Does it work now?

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,657

    Re: Error in IF statement

    When mixing And/AndAlso with Or/OrElse, use brackets where appropriate to indicate how they should be grouped (this ensures the behaviour you want, and helps readability).

    Your If statement could be interpreted in either of these ways:
    Code:
    If ((CmbLateStart.Visible = True AndAlso CmbLateStart.SelectedItem = "---") OrElse CmbLateStart.Text = vbNullString) Then
    (either the last condition must be true, or both of the others)
    Code:
    If (CmbLateStart.Visible = True AndAlso (CmbLateStart.SelectedItem = "---" OrElse CmbLateStart.Text = vbNullString)) Then
    (the first condition must be true, plus either of the others)


    Based on what you have written, it sounds like it is being interpreted as the first way, but you wanted the second way.

  4. #4
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    624

    Re: Error in IF statement

    The "And" has priority over the "Or" so without bracket it is interpreted like that : X and Y or Z equivalent to (X and Y) or Z. So as long as you condition after the OrElse is true, you will activate what is in the "then" part.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    28

    Re: Error in IF statement

    Thanks to all for responding, this is a great learning experience. Good things to keep in mind while I am creating these kinds of statements.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,143

    Re: Error in IF statement

    Operator precedence, especially for Boolean and bitwise operators, can appear a bit fiddly. Even if you look them up, you might misinterpret the significance of the order for OrElse and AndAlso. The higher order operator will go first, but some sites can make them look equivalent. That's never the case. Two operators simply can't have the same precedence, because one will always have to go ahead of the other if both are found together. In this case, AndAlso gets the nod over OrElse, but it wouldn't be hard to miss out on that, or get it backwards, even if you look it up. Parentheses are your friend. They remove any ambiguity as to what you WANT to have happen, which can be a good hint for your future self. After all, we never intentionally create bugs (or at least MOST of us don't), so when you come back to code looking for one, the placement of parentheses can help you remember what you were expecting to have happen. For this reason, I sometimes add parentheses in equations even when the order of operations is well known. For example, I might write (x * 5) + 6, even though x * 5 + 6 will have the exact same result, since multiplication has clear precedence over addition. If my equation is a theoretical solution to a certain problem, I want that hint in there to remind me what my thinking about the theoretical equation was. Comments are also good for that, but parentheses are sometimes even more clear.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2021
    Posts
    28

    Re: Error in IF statement

    Thanks for the detailed explanation "Shaggy Hiker". This is great information as this does seem to get a little muddy and unclear at times. I appreciate your time in explaining this.

Tags for this Thread

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