Вы находитесь на странице: 1из 16

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

com

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?
VBA = Visual Basic for Applications Visual Basic is a stand-alone programming software that is event-driven VB for Applications = Visual Basic that is customized to work with Access, Excel, Word, etc.

Excel has Sheet objects Word has Document objects Access has Data objects

Brief History

Visual Basic for Applications (VBA) is derived from Visual Basic (VB)
VBA was fully integrated into Office 97 except Outlook Prior to that, there were Macros

Before VB, there was QBasic (Microsoft Products)


Based on BASIC (Beginners All-purpose Symbolic Instruction Code) programming language

Brief History

Languages based on BASIC got a bad rap because:


They were considered slow Need a software platform to run

Brief History
Software Platform for VB

VB
Windows

CPU

Brief History
Software Platform for VBA

VBA
Access Windows CPU

Event-Driven Programming

What is it?
Code does not execute until an event is happening
Analogy: You answer the phone when it rings instead of picking up the receiver every 2 seconds to find out if somebody is calling.

Program is Form-centric
Code uses forms, which breaks up code into smaller portions, and gives the user flexibility when entering data.

Helpful Hints

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

Strive to make your code as short as possible


It saves time when typing code Runs faster Easier to debug

Save Often (Ctrl + S)

Variables and Constants

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

A constant is a storage for a value that does not change during code execution
3.1415, vbYes, vbRed Can be changed manually
Going from 365 days to 360 days when calculating interest

Variables and Constants

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

And there are many more!!!!

Variables and Constants

We will work mostly with (for starters):


Strings Integers (whole numbers, no decimals) Boolean Variant

Variables and Constants

Naming convention for Variables:


Strings Starts with str
strFirstName

Integers Starts with int


intCount

Boolean Starts with bln, bol, bool


boolExit

Variant Starts with var


varAnyValue

Variables and Constants

A variable needs to be declared Syntax: Dim variablename [As type]


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

Dim strFirstName As String

Variables and Constants

Naming convention for Constants


Well do it in next class!!!! Well also talk about Public and Private variables and scope/visibility Why not making all variables Variants And much more

Review

It works!!! = Good Code


Code is short and sweet = Even better

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