Processing Decisions Chapter 3. If…Then…Else… Statements If decSalesAmount > 1000 Then...

Preview:

Citation preview

Processing Decisions

Chapter 3

If…Then…Else… Statements

• If decSalesAmount > 1000 ThensngTaxRate = .05

• ElsesngTaxRate = .07

• End If

Nested If Statements

• Test for multiple conditions with multiple results.

• Job level is based on salary.

• < 25000 – Administrative

• > 75000 – Senior Managers

• All Others - Managers

Nested If Statements• If decSalary > 25000 Then

– If decSalry > 75000 Then

lblLevel.Text = “Senior Manager”

– Else

lblLevel.Text = “Manager”– End If

• Else– lblLevel.Text = “Administrative”

• End If

Nested If Statements

• If decSalary <= 25000 ThenlblLevel.Text = “Administrative”

• Else– If decSalry <= 75000 Then

lblLevel.Text = “Manager”– Else

lblLevel.Text = “Senior Manager”

– End If

• End If

If ElseIf Else Statements

• If condition Thenstatements

• ElseIf elseifcondition Thenelseifstatements

• Elseelsestatements

• End If

If ElseIf Else Statements

• If decSalary < 25000 ThenlblLevel.Text = “Administrative”

• ElseIf decSalry <= 75000 ThenlblLevel.Text = “Manager”

• ElselblLevel.Text = “Senior Manager”

• End If

If ElseIf Else Example

• Bonus is a percentage of salary depending on their performance rating.

Rating Bonus

3 10%

2 7.5%

1 5%

If ElseIf Else Example

• If intRating = 3 ThensngBonus = .1

• ElseIf intRating = 2 ThensngBonus = .075

• ElseIf intRating = 1 ThensngBonus = .05

• ElsesngBonus = 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

Enumeration Value Description

OKOnly 0 (Default) 1 Button

OKCancel 1 2 Buttons

AbortRetryIgnore 2 3 Buttons

YesNoCancel 3 3 Buttons

YesNo 4 2 Buttons

Retry Cancel 5 2 Buttons

MessageBoxStyle Icon Values

Enumeration Value Description

Critical 16 Critical Icon

Question 32 Question Mark

Exclamation 48 Exclamation Point

Information 64 Information Icon

MessageBoxStyle Default Button Values

Enumeration Value Description

Default Button 1 0 (default)

First Button is Default

Default Button 2 256 Second Button

Default Button 3 512 Third Button

MessageBoxStyle Modality Values

Enumeration Value Description

ApplicationModal 0 (default) User must respond to the message box before continuing with this application.

SystemModal 4096 All applications are suspended until user responds to message box.

MessageBoxStyle Window Settings Values

Enumeration Value Description

MsgBoxSetForeground

65536 Window in front.

MsgBoxRight 524288 Text is Right Aligned.

MsgBoxRtlReading 1048576 Text appears right to left (other language).

Setting Enumerations

• YesNo 4• Question 32• MsgBoxRight 524288• Sum 524324

• intButtons = 524324• 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 Pushed Value Constant

OK 1 vbOK

Cancel 2 vbCancel

Abort 3 vbAbort

Retry 4 vbRetry

Ignore 5 vbIgnore

Yes 6 vbYes

No 7 vbNo

btnExit_Click

Dim intResponse as IntegerintResponse = MsgBox(“Do you

want to exit?”, MsgBoxStyle.Exclamation + MsgBoxStyle.DefaultButton2 + MsgBoxStyle.YesNo, “Exit?)

If intResponse = vbYes ThenEnd

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

Expression Return 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 1 ASCII Value

Value 2 ASCII Value

Value 1 > Value 2

Z 122 A 65 True

+1 43 -1 45 False

A 65 ? 63 True

Missouri 79 Mississippi 73 True

100 49 98 57 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 >= 25000 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 >= 25000 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

Case expressionlist

End Select

Select Case Example

• Bonus is a percentage of salary depending on their performance rating.

Rating Bonus

8-10 10%

4-7 7.5%

1-3 5%

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

Recommended