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?
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.
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.
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.
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.
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.