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 PresentationTRANSCRIPT
![Page 1: Controlling Execution](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/1.jpg)
Controlling Execution
![Page 2: Controlling Execution](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/13.jpg)
Arrays
• Lists (states, income levels, schedules, etc)
• Private, Public, fixed or dynamic size
• Dim ccyIncomeLevel(0 to 5) As Currency
![Page 14: Controlling Execution](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/17.jpg)
Strings
![Page 18: Controlling Execution](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/19.jpg)
' 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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/20.jpg)
' 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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/25.jpg)
InStr()
strHaystack ="XXpXXpXXPXXP“strNeedle = "p"
intWhere = Instr(1, strHaystack , "W") Returns 0
intWhere = Instr(4, strHaystack , strNeedle )
Returns 6
![Page 26: Controlling Execution](https://reader036.vdocuments.net/reader036/viewer/2022081603/5681401c550346895dab73d4/html5/thumbnails/26.jpg)
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))