In previous lessons, we have learned how to create Visual Basic code that can accept input from the user and display the output without controlling the program flow. In this chapter, you will learn how to create VB code that can make a decision when it process input from the user, and control the program flow in the process. Decision making process is an important part of programming because it can help to solve practical problems intelligently so that it can provide useful output or feedback to the user. For example, we can write a program that can ask the computer to perform certain task until a certain condition is met.
To control the VB program flow, we can use various conditional operators. Basically, they resemble mathematical operators. Conditional operators are very powerful tools, they let the VB program compare data values and then decide what action to take, whether to execute a program or terminate the program and more. These operators are shown in Table 7.1.
|>||More than or equal|
|<=||Less than or equal|
|<>||Not Equal to|
In addition to conditional operators, there are a few logical operators that offer added power to the VB programs. They are shown in Table 7.2.
|And||Both sides must be true|
|Or||One side or other must be true|
|Xor||One side or other must be true but not both|
* You can also compare strings with the operators. However, there are certain rules to follow where upper case letters are less than lowercase letters, and number are less than letters.
To effectively control the VB program flow, we shall
use If...Then...Else statement together with the conditional operators and
If conditions Then VB expressions Else VB expressions End If
This program simulates a sign in process. If the username and password are correct, sign in is successful else sign in failed. Start VB6 and insert two textboxes on the form, rename them UsrTxt and pwTxt, the first textbox is to accept username input and the second one for password input. For pwTxt, set the PasswordChr(password characters) property to * so that the password will appear as * instead of the actual character. We have written the code so that both username and password must be correct to enable sign in if either one of them incorrect sign in will fail.
Private Sub OK_Click() Dim username, password As String username = "John123" password = "qwertyupi#@" If UsrTxt.Text = username And pwTxt.Text = password Then MsgBox ("Sign in sucessful") ElseIf UsrTxt.Text <> username Or pwTxt.Text <> password Then MsgBox ("Sign in failed") End If End Sub
You can check out our animated passwords cracker program
This example calculate the commission based on sales volume attained. Let's say the commission structure is laid out as in the table below:
|20000 and above||20|
In this example, we insert a textbox to accept sale volume input and a label to display commission. Insert a command button to trigger the calculation
Private Sub cmdCalComm_Click() Dim salevol, comm As Currency salevol = Val(TxtSaleVol.Text) If salevol >= 5000 And salevol < 10000 Then comm = salevol * 0.05 ElseIf salevol >= 10000 And salevol < 15000 Then comm = salevol * 0.1 ElseIf salevol >= 15000 And salevol < 20000 Then comm = salevol * 0.15 ElseIf salevol >= 20000 Then comm = salevol * 0.2 Else comm = 0 End If LblComm.Caption = Format(comm, "$#,##0.00") End Sub
This is a guess a number game where the user key in a number and see check whether the answer is correct. Thr program will provide a hint whether the number is too small or too big. After a number of trial, the user should get the right answer. The program employ the If..Then..Else technique to check whether the entry is correct.
'Guess a Number Const realNumber = 99 Dim userNumber As Integer Private Sub EXit_Click() End End Sub Private Sub OK_Click() userNumber = entry.Text If userNumber > realNumber Then hint.Caption = "Your number is too big" ElseIf userNumber < realNumber Then hint.Caption = "Your number is too small" entry.Text = "" entry.SetFocus Else hint.Caption = "Congratulation, your number is correct" End If End Sub
The IIf function denotes immediate decision function. It provides a simple decision making process based on three arguments, as follows:
IIf(x, y, z)
x represents a logical expression while y and z represent a numeric or a string expression.
For example, the IIF(x>y, expression 1, expression 2) function evaluates the values of x and y, if x>y. then expression 1 is true, otherwise the expression 2 is true.
Private Sub CmdNumeric_Click() Dim x, y, a, b, ans As Double x = InputBox("Enter a number") y = InputBox("Enter another number") a = Val(x) b = Val(y) ans = IIf(a < b, b - a, a * b) MsgBox ans End Sub Private Sub CmdString_Click() Dim A, B, C As String A = InputBox("Enter a word") B = InputBox("Enter another word") C = IIf(A < B, A, B) MsgBox C End Sub
If you click test string and enter the first word long and the second word short, the logical condition is true, hence the word long will be displayed, as shown in Figure 7.5.
If you click test numeric and enter the first number 200 and the second number 40, the logical condition is false, hence the second expression will be executed, which is 20x40=800, as shown in Figure 7.6.