Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Feb 24, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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 DoEnables
End Sub
Private Sub Text2_Change()
	Call DoEnables
End 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 DoEnables
End Sub
Private 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.
Larry Kehoe
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date