Access VBA Programming for Beginners - Class 4 - by Patrick Lasu p_lasu@lycos.com.

Post on 14-Jan-2016

217 views 2 download

transcript

Access VBA ProgrammingAccess VBA Programmingfor Beginners for Beginners

- Class 4 -- Class 4 -byby

Patrick LasuPatrick Lasu

p_lasu@lycos.comp_lasu@lycos.com

Class 4 - OverviewClass 4 - Overview

Coding ConceptsCoding Concepts– Reading CodeReading Code– VB Operators/Characters:VB Operators/Characters:

» Concatenation, Line Continuation, CommentsConcatenation, Line Continuation, Comments

– Stepping through CodeStepping through Code IF StatementsIF Statements IIF StatementsIIF Statements Select Case StatementsSelect Case Statements

Coding ConceptsCoding Concepts

Reading CodeReading Code– Reading code can be trickyReading code can be tricky

» Statements to the left and right of an equal Statements to the left and right of an equal sign will not be equal until the code is sign will not be equal until the code is executed (and then it can be “not equal” executed (and then it can be “not equal” again).again).

– Tip 1: Read (evaluate) from the right of Tip 1: Read (evaluate) from the right of the equal sign to figure it outthe equal sign to figure it out

– Tip 2: Do not worry about “past” Tip 2: Do not worry about “past” statementsstatements

VBA CodeVBA Code

intValue = 5 + 2intValue = 5 + 2

Next Line of CodeNext Line of Code

Coding ConceptsCoding Concepts

Behind the scenesBehind the scenes

0 = 5 + 20 = 5 + 2

Next Line of CodeNext Line of Code

Reading Code – Example 1Reading Code – Example 1

Behind the scenesBehind the scenes

7 = 5 + 27 = 5 + 2

Next Line of CodeNext Line of Code

VBA CodeVBA Code

intValue = 5 + 2intValue = 5 + 2

Next Line of CodeNext Line of Code

Coding ConceptsCoding Concepts

Reading Code – Example 1Reading Code – Example 1

Behind the scenesBehind the scenes

0 = 50 = 5

0 = 0 + 20 = 0 + 2

Next Line of CodeNext Line of Code

VBA CodeVBA Code

intValue = 5intValue = 5

intValue = intValue + 2intValue = intValue + 2

Next Line of CodeNext Line of Code

Coding ConceptsCoding Concepts

Reading Code – Example 2Reading Code – Example 2

Behind the scenesBehind the scenes

5 = 55 = 5

5 = 5 + 25 = 5 + 2

Next Line of CodeNext Line of Code

VBA CodeVBA Code

intValue = 5intValue = 5

intValue = intValue + 2intValue = intValue + 2

Next Line of CodeNext Line of Code

Coding ConceptsCoding Concepts

Reading Code – Example 2Reading Code – Example 2

Behind the scenesBehind the scenes

7 = 57 = 5

7 = 7 + 27 = 7 + 2

Next Line of CodeNext Line of Code

VBA CodeVBA Code

intValue = 5intValue = 5

intValue = intValue + 2intValue = intValue + 2

Next Line of CodeNext Line of Code

Coding ConceptsCoding Concepts

Reading Code – Example 2Reading Code – Example 2

Coding ConceptsCoding Concepts

Concatenation character: Concatenation character: & (ampersand)& (ampersand)

Used for putting expressions togetherUsed for putting expressions togetherExample: Example: strFirstName = “John”strFirstName = “John”strLastName = “Doe”strLastName = “Doe”strFullName = strFirstName & “ “ & strLastNamestrFullName = strFirstName & “ “ & strLastName

Avoid using ‘+’ to concatenate, it can Avoid using ‘+’ to concatenate, it can produce unexpected resultsproduce unexpected results

Coding ConceptsCoding Concepts

Line continuation character: Line continuation character: _ (underscore)_ (underscore)Example:Example:

Msgbox “This is important“, _Msgbox “This is important“, _vbOKOnly, “My Message”vbOKOnly, “My Message”

String Example:String Example:strMsg= “This is a very important “ & _strMsg= “This is a very important “ & _““message from me!”message from me!”

Limit is 25 lines, or 24 line continuationsLimit is 25 lines, or 24 line continuations

Coding ConceptsCoding Concepts Making commentsMaking comments

– Use ‘ (apostrophe) to start a commentUse ‘ (apostrophe) to start a comment– Comments are not executed; used to document Comments are not executed; used to document

what the code is suppose to dowhat the code is suppose to do» No need to write an essayNo need to write an essay

Example:Example:‘‘Assign a valueAssign a valueintValue=5intValue=5

Rem Example:Rem Example:Rem Assign a valueRem Assign a valueintValue=5intValue=5

Coding ConceptsCoding Concepts

Stepping through codeStepping through code– Use [F5] in the code window to execute the Use [F5] in the code window to execute the

codecode– Use [F8] in the code window to execute the Use [F8] in the code window to execute the

code one step at a timecode one step at a time Works in a Standard Module, does not work Works in a Standard Module, does not work

in a Form Module.in a Form Module.

IF StatementIF Statement

An IF statement evaluates a condition to find An IF statement evaluates a condition to find out if it is True or False, then executes the out if it is True or False, then executes the appropriate statement(s)appropriate statement(s)

Type the word ‘If’ in the code window and Type the word ‘If’ in the code window and press [F1] for help on the topicpress [F1] for help on the topic

IF StatementIF Statement Syntax (Single Line):Syntax (Single Line):

If If conditioncondition Then [ Then [statementsstatements] [Else ] [Else elsestatementselsestatements]] If If Sales>100K Sales>100K Then Then Bonus=10%Bonus=10% Else Else Bonus=1%Bonus=1%

Syntax (Multi-Line)Syntax (Multi-Line)

If If conditioncondition Then Then[[statementsstatements]]

[Else[Else[[elsestatementselsestatements]]]]

End IfEnd If

Pseudo-code:

If Sales>100K Then

Bonus=10%

Else

Bonus=1%

End If

IF StatementIF Statement

Syntax (If…Then…ElseIf…Then…Else)Syntax (If…Then…ElseIf…Then…Else)

If If conditioncondition Then Then[[statementsstatements]]

[ElseIf [ElseIf condition-ncondition-n Then Then[[elseifstatementselseifstatements] ...] ...

[Else[Else[[elsestatementselsestatements]]]]

End IfEnd If

If Sales>100K Then Bonus=10%ElseIf Sales>50K Then Bonus=5%Else Bonus=1%End If

IF StatementIF Statement

Nested IFNested IF

If If conditioncondition Then Then

If If conditioncondition Then Then

[[statementsstatements]]

[Else[Else

[[elsestatementselsestatements]]]]

End IfEnd If

[Else[Else

[[elsestatementselsestatements]]]]

End IfEnd If

Pseudo-Code:

If Sales>100K Then

If NewClients>5 Then

Bonus=15%

Else

Bonus=10%

End If

Else

Bonus=1%

End If

IF StatementIF Statement

Operators for IF Statement Operators for IF Statement conditioncondition– Equal (=)Equal (=)– Not Equal (<>)Not Equal (<>)– Less Than (<)Less Than (<)– Less Than Or Equal To (<=)Less Than Or Equal To (<=)– Greater Than (>)Greater Than (>)– Greater Than Or Equal To (>=)Greater Than Or Equal To (>=)

IF StatementIF Statement

Logical OperatorsLogical Operators– AndAnd– OrOr– NotNot– XorXor– EqvEqv– ImpImp

If Sales>100K And NewClients > 5 Then

If Sales>100K Or NewClients > 10 Then

If Not(Sales>100K) Then

Immediate IFImmediate IF

Immediate If is similar to If statementsImmediate If is similar to If statements– SyntaxSyntax

IIf(IIf(expr, truepart, falsepartexpr, truepart, falsepart))IIf(Sales>100K, Bonus=10%, 1%)IIf(Sales>100K, Bonus=10%, 1%)

– Can be nestedCan be nestedIIf(IIf(expr, truepart, expr, truepart, IIf(IIf(expr, truepart, falsepartexpr, truepart, falsepart))))

IIf(Sales>100K, Bonus=10%, IIf(Sales>50K,IIf(Sales>100K, Bonus=10%, IIf(Sales>50K, Bonus=5%, Bonus=1%))Bonus=5%, Bonus=1%))

Type the word ‘IIf’ in the code window and press [F1] for Type the word ‘IIf’ in the code window and press [F1] for help on the topichelp on the topic

Immediate IFImmediate IF

The drawback is that it always evaluates The drawback is that it always evaluates both true and false partboth true and false part– It is slower than IF statementsIt is slower than IF statements– Can give unexpected resultsCan give unexpected results

Select CaseSelect Case

Select Case is similar to If…Then…ElseIf Select Case is similar to If…Then…ElseIf statements.statements.

The difference is that it evaluates an The difference is that it evaluates an expression once and then compares it to expression once and then compares it to different valuesdifferent values– Makes it more efficient than multiple ElseIf Makes it more efficient than multiple ElseIf

statements statements Type the word ‘Select’ in the code window Type the word ‘Select’ in the code window

and press [F1] for help on the topicand press [F1] for help on the topic

Select CaseSelect Case

Syntax:Syntax:Select Case Select Case testexpressiontestexpression

[Case [Case expressionlist1expressionlist1]][[statementsstatements]]

[Case [Case expressionlist2expressionlist2]][[statementsstatements]]

[Case Else][Case Else][[statementsstatements]]

End SelectEnd Select

Pseudo-Code:

Select Case Select Case SalesSalesCase Case >100K>100K

Bonus=10%Bonus=10%Case Case >50K>50K

Bonus=5%Bonus=5%Case ElseCase Else

Bonus=1%Bonus=1%End SelectEnd Select