Processing Decisions Chapter 3
If…Then…Else… Statements If decSalesAmount > 1000 Then sngTaxRate =.05 Else sngTaxRate =.07 End If
Nested If Statements Test for multiple conditions with multiple results. Job level is based on salary. < – Administrative > – Senior Managers All Others - Managers
Nested If Statements If decSalary > Then –If decSalry > Then lblLevel.Text = “Senior Manager” –Else lblLevel.Text = “Manager” –End If Else –lblLevel.Text = “Administrative” End If
Nested If Statements If decSalary <= Then lblLevel.Text = “Administrative” Else –If decSalry <= Then lblLevel.Text = “Manager” –Else lblLevel.Text = “Senior Manager” –End If End If
If ElseIf Else Statements If condition Then statements ElseIf elseifcondition Then elseifstatements Else elsestatements End If
If ElseIf Else Statements If decSalary < Then lblLevel.Text = “Administrative” ElseIf decSalry <= Then lblLevel.Text = “Manager” Else lblLevel.Text = “Senior Manager” End If
If ElseIf Else Example Bonus is a percentage of salary depending on their performance rating. RatingBonus 310% 27.5% 15%
If ElseIf Else Example If intRating = 3 Then sngBonus =.1 ElseIf intRating = 2 Then sngBonus =.075 ElseIf intRating = 1 Then sngBonus =.05 Else sngBonus = 0 End If
Message Box Function MsgBox “Prompt”,[Buttons], [“Title”] Prompt is the message displayed. Buttons are the choice of buttons and/or icon that appears in the message box. Title is the text on the title bar of the message box.
MessageBoxStyle Values EnumerationValueDescription OKOnly0 (Default)1 Button OKCancel12 Buttons AbortRetryIgnore23 Buttons YesNoCancel33 Buttons YesNo42 Buttons Retry Cancel52 Buttons
MessageBoxStyle Icon Values EnumerationValueDescription Critical16Critical Icon Question32Question Mark Exclamation48Exclamation Point Information64Information Icon
MessageBoxStyle Default Button Values EnumerationValueDescription Default Button 10 (default) First Button is Default Default Button 2256Second Button Default Button 3512Third Button
MessageBoxStyle Modality Values EnumerationValueDescription ApplicationModal0 (default)User must respond to the message box before continuing with this application. SystemModal4096All applications are suspended until user responds to message box.
MessageBoxStyle Window Settings Values EnumerationValueDescription MsgBoxSet Foreground 65536Window in front. MsgBoxRight524288Text is Right Aligned. MsgBoxRtlReading Text appears right to left (other language).
Setting Enumerations YesNo 4 Question 32 MsgBoxRight Sum intButtons = MsgBox( “Yes or No?”, intButtons, “Clarification”)
YesNo 4 Critical 16 DefaultButton2 256 Sum 276 intButtons = 276 MsgBox( “Do you want to exit?”, intButtons, “Exit?”) Setting Enumerations
MessageBox Return Values Button PushedValueConstant OK1vbOK Cancel2vbCancel Abort3vbAbort Retry4vbRetry Ignore5vbIgnore Yes6vbYes No7vbNo
btnExit_Click Dim intResponse as Integer intResponse = MsgBox(“Do you want to exit?”, MsgBoxStyle.Exclamation + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.YesNo, “Exit?) If intResponse = vbYes Then End End IF
Radio Buttons A Radio button is a control for selecting one option from a list of possible options. A Group box assigns a set of radio buttons to a group. A group of radio buttons can only have one button selected. The selected button’s Checked property is set to true.
Grouped Radio Buttons Create the group box first. Create the radio buttons inside the group box. This assures only one radio button in the group can be selected.
Check Box Control A Check box is a control that indicates whether a specific option is selected or not. Any number of check boxes in a group can be selected. Group boxes may or may not be used. A check mark appears in the box. Its Checked property is set to True.
Radio Buttons If rdbAdmin.Checked Then –strLevel = “Administrative” & vbCrLf ElseIf rdbMgr.Checked Then –strLevel = “Manager” & vbCrLf Else –strLevel = “Senior Manager” & vbCrLf End If
Check Boxes If chkMedical.Checked Then –strBenefits = vbCrLf & “Medical Insurance” End If If chkLife.Checked Then –strBenefits = strBenefits & vbCrLf & “Life Insurance” End If If chk401K.Checked Then –strBenefits = strBenefits & vbCrLf & “401K” End If
Data Validation Missing Data If txtData.Text <> “” Then –MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”) Else –MsgBox(“Data is required. Please try again.”, MsgBoxStyle.Critical, “Invalid Data”) End If
IsNumeric Function If the Val function encounters empty or invalid text string it converts the string to 0. IsNumeric function checks a text string and determines if it can be evaluated as a number. Returns a value of True or False. IsNumeric(expression)
Data Validation Non-numeric Data If IsNumeric(txtData.Text) Then –MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”) Else –MsgBox(“A numeric value is required. Please try again.”, MsgBoxStyle.Critical, “Invalid Data”) End If
IsNumeric Examples ExpressionReturn Value “123”True “123.01”True “$1000”False “Zero”False “$100,000”False “123 Dollars”False
Data Validation Valid Range If txtData.Text < 160 Then –MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”) Else –MsgBox(“Vacation hours may not exceed 160 per year.”, MsgBoxStyle.Critical, “Invalid Data”) End If
Combining Data Validation If IsNumeric(txtData.Text) Then –If txtData.Text < 160 Then MsgBox(“You entered: “ & txtData.Text, MsgBoxStyle.Information, “Data is Valid”) –Else MsgBox(“Vacation hours may not exceed 160 per year.”, MsgBoxStyle.Critical, “Invalid Data”) –End If Else MsgBox(“A numeric value is required. Please try again.”, MsgBoxStyle.Critical, “Invalid Data”) End If
String Comparisons Database applications use string comparisons to validate input and to sort information. The order of string data is determined by each character’s ASCII value. ASCII (American Standard Code for Information Interchange) is a standard designation of characters and symbols.
String Comparisons String comparisons are made from left to right one character pair at a time. The comparison is terminated when a character is unequal. “Browning” compared to “Brower” The first four pairs are equal. The fifth pair is unequal (n and e).
String Comparisons Value 1ASCII Value Value 2ASCII Value Value 1 > Value 2 Z122A65True False A65?63True Missouri79Mississippi73True False
UCase and LCase Functions Character Case is very important in string comparisons. Upper case letters are < lower case letters. Use LCase or UCase so you do not have to validate all possible user inputs.
Data Validation Specific Input If UCase(txtDept.Text) = “ACCT” Then ElseIf UCase(txtDept.Text) = “ECO” Then Else End If
Logical Operators And – If both conditions are True the result is True. Or – If either condition is True the result is True. Not – The condition is reversed. True becomes False, False becomes True.
Logical If Example In order to qualify for a bonus, an employee must be employed for at least 12 months and earn at least $25,000 annually.
Logical If Example If intMonthsEmployed >= 12 AND decSalary >= Then –Msgbox “You qualify for the employee bonus.” Else –Msgbox “You are not eligible for a bonus.” End If
Logical If Example An employee may qualify for a bonus, when been employed for 18 months or when he earns $25,000 annually.
Logical If Example If intMonthsEmployed >= 18 OR decSalary >= Then –Msgbox “You qualify for the employee bonus.” Else –Msgbox “You are not eligible for a bonus.” End If
Select Case Structure Used when a single expression is tested for multiple values. A Case is an individual condition. Select Case expression Case expressionlist End Select
Select Case Example Bonus is a percentage of salary depending on their performance rating. RatingBonus % % 1-35% 0 0%
Select Case Example Select Case intRating –Case < 1 sngBonusRate = 0 –Case 1 To 3 sngBonusRate =.05 –Case 4 To 7 sngBonusRate =.075 –Case 8 To 10 sngBonusRate =.1 End Select
Select Case Example
Radio Button CheckChanged Event Private mintRdbIndex as Integer Private Sub rdbLowSalary_CheckChanged () –mintRdbIndex = 0 End Sub Private Sub rdbHighSalary_CheckChanged () –mintRdbIndex = 3 End Sub
Select Case Example Select Case mintRdbIndex –Case 0 lblResult.Text = “Your salary is less than $20,000.” –Case 1 lblResult.Text = “Your salary is between $20,000 and $40,000.” –Case 2 lblResult.Text = “Your salary is between $41,000 and $60,000.” –Case 3 lblResult.Text = “Your salary is greater than $60,000.” End Select
Loan Payment Application
Mail Order Shipping Costs