ISM 3230 Introduction to Programming
Chapter 4 Student Learning OBJECTIVES: |
Upon completion of this chapter, you will be able to 1. Use block Ifs to control the flow of logic. 2. Understand and use nested Ifs 3. Read and create flowcharts indicating the logic in a selection process. 4. Evaluate conditions using the relational operators. 5. Combine conditions using And and Or. 6. Test the Value property of option buttons and check boxes. 7. Perform validation on numeric fields 8. Call event procedures from other procedures. 9. Create message boxes to display error conditions. 10. Apply the message box constants. 11. Debug projects using breakpoints, stepping program execution, and displaying intermediate results. |
Index:
Decision Making
Single-line IF statement | Multi-line IF .. Else statement |
If txtUserResponse = “Yes” Then
|
|
Comparison Operators
txtName.text <> "Frockmeister"
Val(txtAge.Text) > 25
optPrintForm.Value = True
chkDiscardStyles.Value = False
txtInterestRate.Text / 12 <= 0.05
IF Statement Structure (One-way)
IF Statement Structure (Two-way)
Example: If Val(txtSat.text) > 600 Then
Else
End If |
IF Statement Structure (Multi-way)
Else Msgbox "Do Not Admit"End If |
Conditions
Comparing Numeric Variables & Constants
If txtPrice.Text = curRetailPrice Then ...
can result in an inaccurate comparison of a string and a currency value. Better to do this:
If Val(txtPrice.Text) = curRetailPrice Then ...
Comparing Strings - the .Text property of text boxes -
If txtLastName.Text = “Smith”
If Lcase(txtLastName.Text) = “smith”
Uppercase / Lowercase Comparisons
If txtLastName.Text = "Smith" Then …
will evaluate to false if the txtLastName contains SmITH or smith. A better way is to always do this type of operation:
If Ucase(txtLastName.Text) = "SMITH" Then ···
If Lcase(txtLastName.Text) = "smith" Then ...
Compound Conditions
If (Ucase(txtLastName) = “SMITH” And Val(txtAge) > 25) Then...
If (Val(txtAge.Text) = 0 Or txtLastName.Text = “”) Then ...
If a1 <= a2 or a3 > a4 and a5 < a6 Then ...
If (curValue > 100) and (curValue < 200) Then
lblMessage.Caption = "Value between 101 and 199"
End If
If ... / Option Buttons / Check Boxes
Examples:
If chkFlag = True Then
imgFlag.Visible = True
If optDisplayForm Then
frmSecond.Show
When using option button controls, you don't have to compare it against True or False in a conditional expression. This is because the value of 0 is considered False and all other values (1, for example) are considered True. So, instead of writing this:
If optPrintSelection = True Then ···
you can write the following equivalent statement:
If optPrintSelection Then ···
Option Button Example | Check Box Example |
Displaying Messages in Message boxes
A Message Box is a special window displaying a message to the user.
Form:
MsgBox “message” [,buttons][, “t.b. caption”]
Example:
MsgBox “Numeric ID only”, vbOkOnly, “Error”
Buttons you can use:
Displaying a Message String
MsgBox stMessage, vbOKOnly, stTitle
Dim stMessageString as String
Dim stTitleBarString as String
stMessageString = "The top salary is: " & vbCrLf & curTopSal & " for 2001"
stTitleBarString = "Information for You"
MsgBox stMessageString, vbOkOnly, stTitleBarString
iReturnedValue = MessageBox("Click a Button", vbYesNoCancel,"Test")
Message Box Return Values
Input Validation
If IsNumeric(txtQty.Text) Then
lblDue.Caption = curPrice + Val(txtQty)
If Val(txtHours.Text) > 10 And Val(txtHours.Text) <= 80 Then ...
If IsDate(txtData) Then …
If VarType(varValue) = 0 Then...
Calling Event Procedures
Example: Call cmdCalculate_Click
cmdCalculate_Click
Hands on Programming Example
Debugging VB Projects
Chapter Summary
STATEMENTS |
FUNCTIONS |
|
Call |
IsNumeric |
|
If…Then |
Lcase |
|
Else |
Ucase |
|
ElseIf |
||
End If |
||
MsgBox |