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.
Open a form and set the border style to 1-Fixed Single.
Change the MinButton property to True.
Name the form frmConditions.
Apply objects to the form according to the image above. Notice the command button's access key.
Use the Caption property to change the captions of the checkboxes above.
Name the checkboxes chkCondition1 and chkCondition2 accordingly.
Name the command buttons cmdShow and cmdPrompt.
Lock the controls
Recall: Format > Lock Controls.
OR
Right-click on the form and select Lock Controls.
Now lets do some coding
Double-click on cmdShow, press tab, then type:
If chkCondition1.Value = Checked Then
frmCondition.Caption = "Now I've done an If Then!"
Exit Sub
End If
frmCondition.Caption = "Now I've changed" 'Displays "Now I've changed"
in the forms caption
Try the application if you would like.
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.
Type the following line below under End If in cmdShow:
If chkCondition2.Value = 1 Then
MsgBox "Hello there! Now you can great all your
friends!", 64, "Now what?"
frmCondition.Caption = "That was a new message"
Exit Sub
End If
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.
Enter a new line at the top of the sub-procedure cmdShow.
Type the following lines:
Dim strTitle as String
If (chkCondition1.Value And chkCondition2.Value) = 1 Then
strTitle = InputBox("Enter a new title", "Now lets
change the title")
frmCondition.Caption = strTitle
Exit Sub
End If
Run the application and test the values.
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.
Double-click on cmdPrompt, press tab, then type the following lines
Dim strGuess as String, strPassword as String
strPassword = "Forgot"
Prompt:
strGuess = InputBox("Enter 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
Below the strGuess = InputBox("Enter password") line type the following lines:
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!