Visual Basic Programming

Page 9

I hope you are just loving this programming experience as much as I am writing this tutorial! Now for some more programming fundamentals. Specifically, If, Then, Else statements.

The last line of code you entered is explained in the comment I provided. The first statement: If chkCondition1.Value = Checked Then frmCondition.Caption = "Now I've done an If Then!" simply says that if the checkbox chkCondition1 is checked, then display "Now I've done an If Then!" in the form's caption. When using If Then statements, you can specify multiple commands for one condition. The End If command simply lets the program know what statements to use with the condition. The Exit Sub keywords simply tells the program to leave the sub-procedure. Otherwise, the caption in the form will display "Now I've changed" in the caption as specified in the last line above.

Now for our next checkbox. This time, we will display a Message box, or dialog box to the user using the MsgBox function. Message boxes are one way for the program to interact more with the user.

The line above simply states that if the checkbox chkCondition2 is Checked, then display a Message Box. The format above is what to use for the Message Box. Ignore the pop-up that Visual Basic uses. It does not make complete sense. The first part within the parenthesis is the message you see in the message body. The number refers to the type of message box the user will see; in this case, an exclamation. The last part in quotes is the caption for the message box. The code then exits the sub procedure if true.

Now we will display another dialogue called the input box.

The statements you just added check to see if both checkboxes are checked. They need to be tested first and grouped in parenthesis to test the whole expression. Otherwise, the next if statement will test to see if chkCondition1 is checked. The command will then leave the sub-procedure, which it needs to as explained in the first explanation of the code above. We used the "strTitle=" before the commands because the computer needs a variable to store the data into the InputBox function. In the input box, strTitle stores the text you entered, then writes it to the form caption as you entered into the input box.

So what about this Else word. Well, it just means otherwise. Ex: If it rains, then close the windows. Otherwise (Else) keep them open. The ideal place to start with else is with a password.

The code you entered uses local variables strPassword and strGuess to store data in as Strings. These variables only apply to this sub-procedure.
Password stores the string (any text) "Forgot" and strGuess stores whatever the user enters into the input box. Now for the If Then Else. The line Prompt: is a label that we will use in the next step

If strGuess = strPassword Then
    Exit Sub
Else
    MsgBox "Incorrect password", 64, "Error"
    GoTo Prompt
End If

This code simply says that if strGuess (what you entered into the input box) is equal to the variable Password (Forgot), then leave the procedure. Otherwise, display the message box as seen. The 64 in the message box uses the exclamation format for the box. "Error" is the caption; "Incorrect Password" is the prompt. The next line you typed tells the computer to go to the label Prompt above the code, thus looping (repeating) the input box function. EndIf ends the If statement. Be sure that when you type in the password that you are typing Forgot as it is stored in the variable, because it is case-sensitive.

Now you have finished Conditional Statements and can move onto Rollovers!

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30