SlideShare a Scribd company logo
Access VBA Programming
     for Beginners
       - Class 4 -
             by
        Patrick Lasu
     p_lasu@lycos.com
Class 4 - Overview
   Coding Concepts
    – Reading Code
    – VB Operators/Characters:
       » Concatenation, Line Continuation, Comments
    – Stepping through Code
 IF Statements
 IIF Statements
 Select Case Statements
Coding Concepts
 Reading   Code
  – Reading code can be tricky
    » Statements to the left and right of an equal
      sign will not be equal until the code is
      executed (and then it can be “not equal”
      again).
  – Tip 1: Read (evaluate) from the right of
    the equal sign to figure it out
  – Tip 2: Do not worry about “past”
    statements
Coding Concepts
Reading Code – Example 1
VBA Code             Behind the scenes

intValue = 5 + 2     0=5+2
Next Line of Code    Next Line of Code
Coding Concepts
Reading Code – Example 1

VBA Code             Behind the scenes

intValue = 5 + 2     7=5+2
Next Line of Code    Next Line of Code
Coding Concepts
Reading Code – Example 2

VBA Code                   Behind the scenes

intValue = 5               0=5
intValue = intValue + 2    0=0+2
Next Line of Code          Next Line of Code
Coding Concepts
Reading Code – Example 2

VBA Code                   Behind the scenes

intValue = 5               5=5
intValue = intValue + 2    5=5+2
Next Line of Code          Next Line of Code
Coding Concepts
Reading Code – Example 2

VBA Code                   Behind the scenes

intValue = 5               7=5
intValue = intValue + 2    7=7+2
Next Line of Code          Next Line of Code
Coding Concepts
 Concatenation character:
   & (ampersand)
 Used for putting expressions together
    Example:
    strFirstName = “John”
    strLastName = “Doe”
    strFullName = strFirstName & “ “ & strLastName
   Avoid using „+‟ to concatenate, it can
    produce unexpected results
Coding Concepts
   Line continuation character:
     _ (underscore)
    Example:
       Msgbox “This is important“, _
       vbOKOnly, “My Message”
    String Example:
       strMsg= “This is a very important “ & _
       “message from me!”
   Limit is 25 lines, or 24 line continuations
Coding Concepts
   Making comments
    – Use „ (apostrophe) to start a comment
    – Comments are not executed; used to document
      what the code is suppose to do
       » No need to write an essay
    Example:
    „Assign a value
    intValue=5
    Rem Example:
    Rem Assign a value
    intValue=5
Coding Concepts
   Stepping through code
    – Use [F5] in the code window to execute the
      code
    – Use [F8] in the code window to execute the
      code one step at a time
   Works in a Standard Module, does not work
    in a Form Module.
IF Statement
 An IF statement evaluates a condition to find
  out if it is True or False, then executes the
  appropriate statement(s)
 Type the word „If‟ in the code window and
  press [F1] for help on the topic
IF Statement
   Syntax (Single Line):
If condition Then [statements] [Else elsestatements]
If Sales>100K Then Bonus=10% Else Bonus=1%

   Syntax (Multi-Line)     Pseudo-code:
     If condition Then      If Sales>100K Then
        [statements]          Bonus=10%
     [Else                  Else
        [elsestatements]]     Bonus=1%
     End If                 End If
IF Statement
   Syntax (If…Then…ElseIf…Then…Else)

    If condition Then           If Sales>100K Then
       [statements]               Bonus=10%
    [ElseIf condition-n Then    ElseIf Sales>50K Then
       [elseifstatements] ...     Bonus=5%
    [Else                       Else
       [elsestatements]]           Bonus=1%
    End If                      End If
IF Statement
   Nested IF                    Pseudo-Code:
     If condition Then           If Sales>100K Then
         If condition Then          If NewClients>5 Then
             [statements]              Bonus=15%
         [Else                      Else
             [elsestatements]]         Bonus=10%
         End If                     End If
     [Else                       Else
         [elsestatements]]          Bonus=1%
     End If                      End If
IF Statement
   Operators for IF Statement condition
    –   Equal (=)
    –   Not Equal (<>)
    –   Less Than (<)
    –   Less Than Or Equal To (<=)
    –   Greater Than (>)
    –   Greater Than Or Equal To (>=)
IF Statement
   Logical Operators
    –   And     If Sales>100K And NewClients > 5 Then
    –   Or      If Sales>100K Or NewClients > 10 Then
    –   Not     If Not(Sales>100K) Then
    –   Xor
    –   Eqv
    –   Imp
Immediate IF
   Immediate If is similar to If statements
    – Syntax
       IIf(expr, truepart, falsepart)
       IIf(Sales>100K, Bonus=10%, 1%)
    – Can be nested
IIf(expr, truepart, IIf(expr, truepart, falsepart))
IIf(Sales>100K, Bonus=10%, IIf(Sales>50K, Bonus=5%, Bonus=1%))

   Type the word „IIf‟ in the code window and press [F1] for
    help on the topic
Immediate IF
   The drawback is that it always evaluates
    both true and false part
    – It is slower than IF statements
    – Can give unexpected results
Select Case
 Select Case is similar to If…Then…ElseIf
  statements.
 The difference is that it evaluates an
  expression once and then compares it to
  different values
    – Makes it more efficient than multiple ElseIf
      statements
   Type the word „Select‟ in the code window
    and press [F1] for help on the topic
Select Case
 Syntax:                    Pseudo-Code:
Select Case testexpression   Select Case Sales
  [Case expressionlist1]        Case >100K
       [statements]                 Bonus=10%
  [Case expressionlist2]        Case >50K
       [statements]                 Bonus=5%
  [Case Else]                   Case Else
       [statements]                 Bonus=1%
End Select                   End Select

More Related Content

Viewers also liked

Greenwood Transit
Greenwood Transit Greenwood Transit
Greenwood Transit
katemartinseattle
 
Digital moms coke
Digital moms   cokeDigital moms   coke
Digital moms coke
divyalulla1
 
Carta 1
Carta 1Carta 1
Carta 1
Erika Chuga
 
Pp lect 4 holly move image
Pp lect 4 holly move imagePp lect 4 holly move image
Pp lect 4 holly move image
benno91
 
Departmental
DepartmentalDepartmental
Departmental
eewilson
 
Roque
RoqueRoque
PDHPE Rationale
PDHPE RationalePDHPE Rationale
PDHPE Rationale
benno91
 
E business updated
E business updatedE business updated
E business updated
khoslajace
 
Construccion social de la masculinidad y feminidad
Construccion social de la masculinidad y feminidadConstruccion social de la masculinidad y feminidad
Construccion social de la masculinidad y feminidad
Julián Grandson
 

Viewers also liked (10)

Taller power point
Taller power pointTaller power point
Taller power point
 
Greenwood Transit
Greenwood Transit Greenwood Transit
Greenwood Transit
 
Digital moms coke
Digital moms   cokeDigital moms   coke
Digital moms coke
 
Carta 1
Carta 1Carta 1
Carta 1
 
Pp lect 4 holly move image
Pp lect 4 holly move imagePp lect 4 holly move image
Pp lect 4 holly move image
 
Departmental
DepartmentalDepartmental
Departmental
 
Roque
RoqueRoque
Roque
 
PDHPE Rationale
PDHPE RationalePDHPE Rationale
PDHPE Rationale
 
E business updated
E business updatedE business updated
E business updated
 
Construccion social de la masculinidad y feminidad
Construccion social de la masculinidad y feminidadConstruccion social de la masculinidad y feminidad
Construccion social de la masculinidad y feminidad
 

Similar to Vba class 4

Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
Abhishek Kesharwani
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
SzeChingChen
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
Abhishek Kesharwani
 
If statemet1
If statemet1If statemet1
If statemet1
sup11
 
Decisions
DecisionsDecisions
Decisions
nicky_walters
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statement
Eyelean xilef
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statement
Eyelean xilef
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
NewsMogul
 
Decisions
DecisionsDecisions
Decisions
nicky_walters
 
Decisions
DecisionsDecisions
Decisions
nicky_walters
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
maznabili
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
Ben Miu CIM® FCSI A+
 
CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
Hamad Odhabi
 
TDD As If You Meant It
TDD As If You Meant ItTDD As If You Meant It
TDD As If You Meant It
Alexandru Bolboaca
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
Aimee Maree Forsstrom
 
Vb scripting
Vb scriptingVb scripting
Vb scripting
Brad Winborg
 
Na50 enus devi_14
Na50 enus devi_14Na50 enus devi_14
Na50 enus devi_14
thurmond
 
if statements in Python -A lecture class
if statements in Python -A lecture classif statements in Python -A lecture class
if statements in Python -A lecture class
binzbinz3
 
Test driven development
Test driven developmentTest driven development
Test driven development
Suresh Thammishetty
 

Similar to Vba class 4 (20)

Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
If statemet1
If statemet1If statemet1
If statemet1
 
Decisions
DecisionsDecisions
Decisions
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statement
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statement
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
Decisions
DecisionsDecisions
Decisions
 
Decisions
DecisionsDecisions
Decisions
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
CIS 1403 lab 4 selection
CIS 1403 lab 4 selectionCIS 1403 lab 4 selection
CIS 1403 lab 4 selection
 
TDD As If You Meant It
TDD As If You Meant ItTDD As If You Meant It
TDD As If You Meant It
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
 
Vb scripting
Vb scriptingVb scripting
Vb scripting
 
Na50 enus devi_14
Na50 enus devi_14Na50 enus devi_14
Na50 enus devi_14
 
if statements in Python -A lecture class
if statements in Python -A lecture classif statements in Python -A lecture class
if statements in Python -A lecture class
 
Test driven development
Test driven developmentTest driven development
Test driven development
 

Recently uploaded

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
giancarloi8888
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdfREASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
REASIGNACION 2024 UGEL CHUPACA 2024 UGEL CHUPACA.pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 

Vba class 4

  • 1. Access VBA Programming for Beginners - Class 4 - by Patrick Lasu p_lasu@lycos.com
  • 2. Class 4 - Overview  Coding Concepts – Reading Code – VB Operators/Characters: » Concatenation, Line Continuation, Comments – Stepping through Code  IF Statements  IIF Statements  Select Case Statements
  • 3. Coding Concepts  Reading Code – Reading code can be tricky » Statements to the left and right of an equal sign will not be equal until the code is executed (and then it can be “not equal” again). – Tip 1: Read (evaluate) from the right of the equal sign to figure it out – Tip 2: Do not worry about “past” statements
  • 4. Coding Concepts Reading Code – Example 1 VBA Code Behind the scenes intValue = 5 + 2 0=5+2 Next Line of Code Next Line of Code
  • 5. Coding Concepts Reading Code – Example 1 VBA Code Behind the scenes intValue = 5 + 2 7=5+2 Next Line of Code Next Line of Code
  • 6. Coding Concepts Reading Code – Example 2 VBA Code Behind the scenes intValue = 5 0=5 intValue = intValue + 2 0=0+2 Next Line of Code Next Line of Code
  • 7. Coding Concepts Reading Code – Example 2 VBA Code Behind the scenes intValue = 5 5=5 intValue = intValue + 2 5=5+2 Next Line of Code Next Line of Code
  • 8. Coding Concepts Reading Code – Example 2 VBA Code Behind the scenes intValue = 5 7=5 intValue = intValue + 2 7=7+2 Next Line of Code Next Line of Code
  • 9. Coding Concepts  Concatenation character: & (ampersand)  Used for putting expressions together Example: strFirstName = “John” strLastName = “Doe” strFullName = strFirstName & “ “ & strLastName  Avoid using „+‟ to concatenate, it can produce unexpected results
  • 10. Coding Concepts  Line continuation character: _ (underscore) Example: Msgbox “This is important“, _ vbOKOnly, “My Message” String Example: strMsg= “This is a very important “ & _ “message from me!”  Limit is 25 lines, or 24 line continuations
  • 11. Coding Concepts  Making comments – Use „ (apostrophe) to start a comment – Comments are not executed; used to document what the code is suppose to do » No need to write an essay Example: „Assign a value intValue=5 Rem Example: Rem Assign a value intValue=5
  • 12. Coding Concepts  Stepping through code – Use [F5] in the code window to execute the code – Use [F8] in the code window to execute the code one step at a time  Works in a Standard Module, does not work in a Form Module.
  • 13. IF Statement  An IF statement evaluates a condition to find out if it is True or False, then executes the appropriate statement(s)  Type the word „If‟ in the code window and press [F1] for help on the topic
  • 14. IF Statement  Syntax (Single Line): If condition Then [statements] [Else elsestatements] If Sales>100K Then Bonus=10% Else Bonus=1%  Syntax (Multi-Line) Pseudo-code: If condition Then If Sales>100K Then [statements] Bonus=10% [Else Else [elsestatements]] Bonus=1% End If End If
  • 15. IF Statement  Syntax (If…Then…ElseIf…Then…Else) If condition Then If Sales>100K Then [statements] Bonus=10% [ElseIf condition-n Then ElseIf Sales>50K Then [elseifstatements] ... Bonus=5% [Else Else [elsestatements]] Bonus=1% End If End If
  • 16. IF Statement  Nested IF Pseudo-Code: If condition Then If Sales>100K Then If condition Then If NewClients>5 Then [statements] Bonus=15% [Else Else [elsestatements]] Bonus=10% End If End If [Else Else [elsestatements]] Bonus=1% End If End If
  • 17. IF Statement  Operators for IF Statement condition – Equal (=) – Not Equal (<>) – Less Than (<) – Less Than Or Equal To (<=) – Greater Than (>) – Greater Than Or Equal To (>=)
  • 18. IF Statement  Logical Operators – And If Sales>100K And NewClients > 5 Then – Or If Sales>100K Or NewClients > 10 Then – Not If Not(Sales>100K) Then – Xor – Eqv – Imp
  • 19. Immediate IF  Immediate If is similar to If statements – Syntax IIf(expr, truepart, falsepart) IIf(Sales>100K, Bonus=10%, 1%) – Can be nested IIf(expr, truepart, IIf(expr, truepart, falsepart)) IIf(Sales>100K, Bonus=10%, IIf(Sales>50K, Bonus=5%, Bonus=1%))  Type the word „IIf‟ in the code window and press [F1] for help on the topic
  • 20. Immediate IF  The drawback is that it always evaluates both true and false part – It is slower than IF statements – Can give unexpected results
  • 21. Select Case  Select Case is similar to If…Then…ElseIf statements.  The difference is that it evaluates an expression once and then compares it to different values – Makes it more efficient than multiple ElseIf statements  Type the word „Select‟ in the code window and press [F1] for help on the topic
  • 22. Select Case  Syntax: Pseudo-Code: Select Case testexpression Select Case Sales [Case expressionlist1] Case >100K [statements] Bonus=10% [Case expressionlist2] Case >50K [statements] Bonus=5% [Case Else] Case Else [statements] Bonus=1% End Select End Select