16
Access VBA Programming Access VBA Programming for Beginners for Beginners - Class 1 - - Class 1 - by by Patrick Lasu Patrick Lasu [email protected] [email protected]

Access VBA Programming for Beginners - Class 1 -

Embed Size (px)

DESCRIPTION

Access VBA Programming for Beginners - Class 1 -. by Patrick Lasu [email protected]. Class 1 - Overview. What is VBA? History of VBA Event-Driven Programming Helpful Hints Code Window Help in VBA Subs and Functions Variables and Constants. What is VBA?. - PowerPoint PPT Presentation

Citation preview

Page 1: Access VBA Programming for Beginners  - Class 1 -

Access VBA ProgrammingAccess VBA Programmingfor Beginners for Beginners

- Class 1 -- Class 1 -

byby

Patrick LasuPatrick Lasu

[email protected][email protected]

Page 2: Access VBA Programming for Beginners  - Class 1 -

Class 1 - OverviewClass 1 - Overview

What is VBA?What is VBA? History of VBAHistory of VBA Event-Driven ProgrammingEvent-Driven Programming Helpful HintsHelpful Hints Code WindowCode Window Help in VBAHelp in VBA Subs and FunctionsSubs and Functions Variables and ConstantsVariables and Constants

Page 3: Access VBA Programming for Beginners  - Class 1 -

What is VBA?What is VBA?

VBA = Visual Basic for ApplicationsVBA = Visual Basic for Applications Visual Basic is a stand-alone programming Visual Basic is a stand-alone programming

software that is event-drivensoftware that is event-driven VB for Applications = Visual Basic that is VB for Applications = Visual Basic that is

“customized” to work with Access, Excel, “customized” to work with Access, Excel, Word, etc.Word, etc.– Excel has Sheet objectsExcel has Sheet objects– Word has Document objectsWord has Document objects– Access has Data objectsAccess has Data objects

Page 4: Access VBA Programming for Beginners  - Class 1 -

Brief HistoryBrief History

Visual Basic for Applications (VBA) is Visual Basic for Applications (VBA) is derived from Visual Basic (VB)derived from Visual Basic (VB)– VBA was fully integrated into Office –97 VBA was fully integrated into Office –97

except Outlookexcept Outlook– Prior to that, there were MacrosPrior to that, there were Macros

Before VB, there was QBasic (Microsoft Before VB, there was QBasic (Microsoft Products)Products)– Based on BASIC (Beginners All-purpose Based on BASIC (Beginners All-purpose

Symbolic Instruction Code) programming Symbolic Instruction Code) programming languagelanguage

Page 5: Access VBA Programming for Beginners  - Class 1 -

Brief HistoryBrief History

Languages based on BASIC got a bad rap Languages based on BASIC got a bad rap because:because:– They were considered slowThey were considered slow– Need a software platform to runNeed a software platform to run

Page 6: Access VBA Programming for Beginners  - Class 1 -

Brief HistoryBrief History

CPU

Windows

VB

Software Platform for VB

Page 7: Access VBA Programming for Beginners  - Class 1 -

Brief HistoryBrief History

CPU

Windows

Access

VBA

Software Platform for VBA

Page 8: Access VBA Programming for Beginners  - Class 1 -

Event-Driven ProgrammingEvent-Driven Programming

What is it?What is it?– Code does not execute until an event is Code does not execute until an event is

happeninghappening» Analogy: You answer the phone when it rings Analogy: You answer the phone when it rings

instead of picking up the receiver every 2 seconds to instead of picking up the receiver every 2 seconds to find out if somebody is calling.find out if somebody is calling.

– Program is Form-centricProgram is Form-centric» Code uses forms, which breaks up code into smaller Code uses forms, which breaks up code into smaller

portions, and gives the user flexibility when entering portions, and gives the user flexibility when entering data.data.

Page 9: Access VBA Programming for Beginners  - Class 1 -

Helpful HintsHelpful Hints

There are at least 3 ways of accomplishing There are at least 3 ways of accomplishing the same task when codingthe same task when coding– Good Code = It worksGood Code = It works– Bad Code = It does not workBad Code = It does not work

Strive to make your code as short as Strive to make your code as short as possiblepossible– It saves time when typing codeIt saves time when typing code– Runs fasterRuns faster– Easier to debugEasier to debug

Save Often (Ctrl + S)Save Often (Ctrl + S)

Page 10: Access VBA Programming for Beginners  - Class 1 -

Variables and ConstantsVariables and Constants

A variable is a storage for a value that can A variable is a storage for a value that can change during code executionchange during code execution– Answering Yes or NoAnswering Yes or No

A constant is a storage for a value that does A constant is a storage for a value that does not change during code executionnot change during code execution– 3.1415, vbYes, vbRed3.1415, vbYes, vbRed– Can be changed manuallyCan be changed manually

» Going from 365 days to 360 days when calculating Going from 365 days to 360 days when calculating interestinterest

Page 11: Access VBA Programming for Beginners  - Class 1 -

Variables and ConstantsVariables and Constants

There are several Data Types for Variables There are several Data Types for Variables and Constants for efficiencyand Constants for efficiency– String = Stores Text – “Patrick”, “123 Main St”String = Stores Text – “Patrick”, “123 Main St”– Number = Stores Numbers - “1, 2, 3,...”, “3.14”Number = Stores Numbers - “1, 2, 3,...”, “3.14”– Boolean = Stores True/FalseBoolean = Stores True/False– Date = Stores DateDate = Stores Date– Currency = Currency format – “Dollar, Yen”Currency = Currency format – “Dollar, Yen”– Variant = Stores AnythingVariant = Stores Anything

And there are many more!!!!And there are many more!!!!

Page 12: Access VBA Programming for Beginners  - Class 1 -

Variables and ConstantsVariables and Constants

We will work mostly with (for starters):We will work mostly with (for starters):– StringsStrings– Integers (whole numbers, no decimals)Integers (whole numbers, no decimals)– BooleanBoolean– VariantVariant

Page 13: Access VBA Programming for Beginners  - Class 1 -

Variables and ConstantsVariables and Constants

Naming convention for Variables:Naming convention for Variables:– Strings – Starts with “str”Strings – Starts with “str”

» strFirstNamestrFirstName

– Integers – Starts with “int”Integers – Starts with “int”» intCountintCount

– Boolean – Starts with “bln”, “bol”, “bool”Boolean – Starts with “bln”, “bol”, “bool”» boolExitboolExit

– Variant – Starts with “var”Variant – Starts with “var”» varAnyValuevarAnyValue

Page 14: Access VBA Programming for Beginners  - Class 1 -

Variables and ConstantsVariables and Constants

A variable needs to be declaredA variable needs to be declared

Syntax: Dim Syntax: Dim variablenamevariablename [As type] [As type]

– Dim = Dimension (make space for it)Dim = Dimension (make space for it)– variablename = Ex: strFirstNamevariablename = Ex: strFirstName– [As type] = Optional, Ex: As String[As type] = Optional, Ex: As String

Dim strFirstName As StringDim strFirstName As String

Page 15: Access VBA Programming for Beginners  - Class 1 -

Variables and ConstantsVariables and Constants

Naming convention for ConstantsNaming convention for Constants– We’ll do it in next class!!!!We’ll do it in next class!!!!– We’ll also talk about Public and Private We’ll also talk about Public and Private

variables and scope/visibilityvariables and scope/visibility– Why not making all variables VariantsWhy not making all variables Variants– And much more…And much more…

Page 16: Access VBA Programming for Beginners  - Class 1 -

ReviewReview

It works!!! = Good CodeIt works!!! = Good Code– Code is short and sweet = Even better…Code is short and sweet = Even better…

There are at least 3 different ways of codingThere are at least 3 different ways of coding Save OftenSave Often Use the Help filesUse the Help files