Multiply Conditions for Boolean Result

You often have to test for multiple conditions when enabling a confirmation button or other control that commits user input. Instead of using complex If…ElseIf statements or inline If functions, you can manage multiple conditions by multiplying the many conditions together. This way, any condition that hasn’t been met evaluates to zero and the rules of multiplication will keep your confirmation control disabled. For example, assume you have two textboxes that must contain text before enabling a command button. Call a common subroutine in each textbox’s Change event:

 Private Sub Text1_Change()	Call DoEnablesEnd SubPrivate Sub Text2_Change()	Call DoEnablesEnd Sub

In the common subroutine, set the command button’s Enabled property:

 Private Sub DoEnables	cmdOk.Enabled = Len(Trim$(Text1)) * Len(Trim$(Text2))End Sub

Also, adding new conditions is a simple task. Simply include the new condition into the series of multiplication:

 Private Sub TextMustBeGreaterThan5_Change()	Call DoEnablesEnd SubPrivate Sub DoEnables()	cmdOk.Enabled = Len(Trim$(Text1)) * Len(Trim$(Text2)) * _		(Val(TextMustBeGreaterThan5) > 5)End Sub

You can add as many conditions as you need. Any condition not met evaluates to zero before being included into the equation. Make sure to enclose your logical operators in parentheses. Even one zero results in zero for the entire calculation, which VB treats as False for the Enabled property. If all conditions are met, you get a nonzero number, which VB treats as True.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: