Controlling Execution

26
Controlling Execution

description

Controlling Execution. IF and only if. One line IF IF with and without ENDIF IF with and without ELSE One line ELSEIF Multiple ELSEIFs and Select Case. IF and only if. One line IF (without END IF) IF (condition) Then Exit Sub One seeming line IF IF (condition) Then _ - PowerPoint PPT Presentation

Transcript of Controlling Execution

Page 1: Controlling Execution

Controlling Execution

Page 2: Controlling Execution

IF and only if..

• One line IF

• IF with and without ENDIF

• IF with and without ELSE

• One line ELSEIF

• Multiple ELSEIFs and Select Case

Page 3: Controlling Execution

IF and only if..

• One line IF (without END IF) IF (condition) Then Exit Sub• One seeming line IF IF (condition) Then _ Exit Sub• IF with ENDIF IF (condition) Then Exit Sub END IF

Page 4: Controlling Execution

IF and only if..• One line IF with ELSE IF (whatever) Then Yada ELSE YadaYada

• Five line IF with ELSE IF (whatever) Then Yada ELSE YadaYada END IF

Page 5: Controlling Execution

IF and only if..

• ELSE required alone on one line IF (whatever) Then

Yada

ELSE

YadaYada

END IF

• THEN at end of line required

• END IF required alone on one line

Page 6: Controlling Execution

ElseIF and only if..

• ElseIF is subordinate to the previous IF

IF (whatever) Then

Yada

ElseIF (whatever) Then

YadaYada

ELSE

YadaYadaYada

END IF

Page 7: Controlling Execution

Select Case vs ElseIF

• Select Case neat and maintainable

• Case formats very flexible

• Case 1,3,7

• Case 2 to 8

• Case “CA” to “NM”

• Case Is > 21

Page 8: Controlling Execution

Select Case Example

Select Case SecurityCase "V"

' set properties for volunteers

Case "S"

' set properties for supervisors

Case ""

' exit sub

End Select

Page 9: Controlling Execution

Immediate If!

• IIF is a function

• IIF(condition, true return, false return)

• IIF(blnMale=True,”Male”,”Female”)

• Useful in queries, properties, division

• IIF(intN > 0, 300/intN, 0) ' to avoid an error msg

• IIF(PercentCorrect>=90,"A", _

• IIf(PercentCorrect>=80,"B", _

• IIf(PercentCorrect>=70,"C", _

• IIf(PercentCorrect>=60,"D","F"))))

Page 10: Controlling Execution

For Loops

• For intX = 1 to 10 Step 2 • For Each varX in varDataSet

– Next required for these two For formats

• Exit For

Restrictions on For Each…• 1-dimension only• Each control variable must be Variant

Page 11: Controlling Execution

For Loops: Examples

Dim varDataSet(0 to 49) as VariantDim intX as Integer, intOdd as IntegerDim intSum as IntegerDim varItem as Variant, varSum as Variant' sum the odd numbered elements (1,3,5,…)For intX = 1 to 49 Step 2

intOdd = intOdd + varData(intX)Next' sum all elements (1,2,3,…) For Each varItem in varDataSet intSum = intSum + varItemNext

Page 12: Controlling Execution

Do Loops

Conditional logic after 1 pass through loop• Do … Loop While• Do … Loop Until

Or, conditional logic before 1st pass through loop• Do While … Loop• Do Until … Loop

Exit Do

Page 13: Controlling Execution

Arrays

• Lists (states, income levels, schedules, etc)

• Private, Public, fixed or dynamic size

• Dim ccyIncomeLevel(0 to 5) As Currency

Page 14: Controlling Execution

Which Array to Go?

• Option Base 1 or Option Base 0?

• Public strState(0 to 49) As String

• Public strState(1 to 50) As String

• Public strState(50) As String– Above will be (0 to 49 or 1 to 50)

depending on Option Base

Page 15: Controlling Execution

strMonth(1) = "January"

strMonth(2) = "February"

strMonth(3) = "March"

strMonth(4) = "April"

strMonth(5) = "May"

intInput = InputBox("Enter Month Number")

MsgBox “You mean “& strMonth(intInput) & “?”

Or MsgBox “You mean “& strMonth(intInput+1) & “?”

Page 16: Controlling Execution

Flexible Array

• Dynamic• Dim ccyLevel() As Currency

• Expands and contracts

• Good for uncertain run-time conditions

• Can loose contents unless…– Redim Preserve ccyLevel(7)

• Can erase dynamic arrays: Erase ccyLevel

Page 17: Controlling Execution

Strings

Page 18: Controlling Execution

Notes on strings

• String variables (Dim strName as String)• String literals (txtName = "Jones")

• Mixing variables and literalsSelect * from tblRoster Where Name = "Smith"

• How to embed quotes around strName?"Select * from tblRoster Where Name =" & ???

Page 19: Controlling Execution

' If the name is a hard-coded literalDebug.Print "Name = 'Smith'"

' If the name is in a variable (very likely)Dim strName As StringstrName = "Smith"

Debug.Print "Name = '" & strName & "'"

How to embed single quotes

Page 20: Controlling Execution

' When the name is a hard-coded literalDebug.Print "LastName = ""Smith"""

'Coding hints for the triple quotes above: 'step 1: LastName = "Smith" 'step 2: LastName = ""Smith"" 'step 3: "LastName = ""Smith"" "

How to embed double quoteswith literal strings

Page 21: Controlling Execution

Dim strName As StringstrName = "Smith"Debug.Print "LastName = """ & strName & """" 'Coding hints for triple/quadruple quotes: 'step 1: "LastName = strName" 'step 2: "LastName = """ & strName " 'step 3: "LastName = """ & strName & """ "

How to embed double quotesusing variables

Page 22: Controlling Execution

Dim strName As StringstrName = "Nguyen"

Dim strQuote As StringstrQuote = Chr(34) ' ACSII for double quote

strName = strQuote & strName & strQuoteDebug.Print "LastName = " & strName

How to embed double quotesas a variable

Page 23: Controlling Execution

String Functions

• mid(strHayStack, intStart, intLength)– Returns string or part of a string

• instr(intStart, strHayStack, strNeedle)– Returns position where needle is in haystack

Page 24: Controlling Execution

Mid()

MyString = "Mid Function Demo" ' Create text string.

FirstWord = Mid(MyString, 1, 3) ' Returns "Mid".

LastWord = Mid(MyString, 14, 4) ' Returns "Demo".

MidWords = Mid(MyString, 5) ' Returns "Function Demo".

Page 25: Controlling Execution

InStr()

strHaystack ="XXpXXpXXPXXP“strNeedle = "p"

intWhere = Instr(1, strHaystack , "W") Returns 0

intWhere = Instr(4, strHaystack , strNeedle )

Returns 6

Page 26: Controlling Execution

Other String Functions

• Left() and Right() ' args: string, no. of chars

• Ucase() and Lcase() ' one arg only

• Trim(), Rtrim() and Ltrim() ' one arg only

• Val(strA) ' change a string to a number

• Cstr(intX) ' change a number to a string

• Because Cstr(57) returns " 57" not "57" use Trim(Cstr(intX))