Visual Basic
Visual Basic
0
What is Visual Basic?
•The "Visual" part refers to the method used
to create the graphical user interface (GUI).
Rather than writing numerous lines of code to
describe the appearance and location of
interface elements, you simply add pre-built
objects into place on screen
•The "Basic" part refers to the BASIC
(Beginners All-Purpose Symbolic Instruction
Code) language
Visual Basic Characteristics
• Rapid Application Development (RAD) tool
• Front-end applications.
•ActiveX technologies allow you to use the functionality
provided by other applications, such as Microsoft Word
word processor, Microsoft Excel spreadsheet, and other
Windows applications.
•Your finished application is a true .exe file.
Event-Driven programming Language?
In an event-driven application, the code doesn't follow a
predetermined path — it executes different code sections in
response to events.
Events can be triggered by the user's actions, by messages
from the system or other applications, or even from the
application itself.
The sequence of these events determines the sequence in
which the code executes, thus the path through the
application's code differs each time the program runs.
Your code can also trigger events during execution. For
example, programmatically changing the text in a text box
cause the text box's Change event to occur.
Interactive Development
The traditional application development process can be broken
into three distinct steps: writing, compiling, and testing code.
Unlike traditional languages, Visual Basic uses an interactive
approach to development, blurring the distinction between the
three steps.
With most languages, if you make a mistake in writing your
code, the error is caught by the compiler when you start to
compile your application. You must then find and fix the error
and begin the compile cycle again, repeating the process for
each error found.
Visual Basic interprets your code as you enter it, catching and
highlighting most syntax or spelling errors on the fly.
In addition to catching errors on the fly, Visual Basic also
partially compiles the code as it is entered. If the compiler
finds an error, it is highlighted in your code. You can fix the
error and continue compiling without having to start over.
Because of the interactive nature of Visual Basic, you'll find
yourself running your application frequently as you develop
it. This way you can test the effects of your code as you
work rather than waiting to compile later.
The Integrated Development Environment
Selecting the Project Type
Working with Visual Basic Projects
A project consists of :
•One project file that keeps track of all the components (.vbp).
•One file for each form (.frm).
•One binary data file for each form containing data for properties of
controls on the form (.frx). These files are not editable and are
automatically generated for any .frm file that contains binary
properties, such as Picture or Icon.
•Optionally, one file for each class module (.cls).
•Optionally, one file for each standard module (.bas).
The project file is simply a list of all the files and objects associated
with the project, as well as information on the environment options
you set.
You can convert the project into an executable file (.exe)
Form Modules
Form modules (.frm file name extension) can contain textual
descriptions of the form and its controls, including their property
settings. They also contain form-level declarations of constants,
variables and procedures; event procedures; and general
procedures.
Class Modules
Class modules (.cls file name extension) are similar to form modules,
except that they have no visible user interface. You can use class
modules to create your own objects, including code for methods and
properties.
Standard Modules
Standard modules (.bas file name extension) can contain public or
module-level declarations of types, constants, variables, external
procedures, and public procedures.
Variables
Variable is a placeholder in memory.
Variables are used to temporarily store values during the execution
of an application.
Variables have a name (the word you use to refer to the value the
variable contains) and a data type (which determines the kind of
data the variable can store).
A variable name:
•Must begin with a letter.
•Can't contain an embedded period.
•Must not exceed 255 characters.
•Must be unique within the same scope, which is the range from
which the variable can be referenced — a procedure, a form, and so
on.
Implicit Declaration
You don't have to declare a variable before using it.
Visual Basic automatically creates a variable with that name, which
you can use as if you had explicitly declared it. While this is
convenient, it can lead to subtle errors in your code if you misspell a
variable name.
Example:
x = “Sam”
a = 45
Explicit Declaration
To avoid the problem of misnaming variables, you can stipulate that
Visual Basic always warn you whenever it encounters a name not
declared explicitly as a variable.
Place this statement in the Declarations section of a class, form, or
Scope of Variables
A variable is scoped as either a procedure-level (local) or module-
level variable.
Scope
Procedure-level
Private: Variables are private to the procedure in which they
appear.
Public: Not applicable. You cannot declare public variables
within a procedure.
Module-level
Private: Variables are private to the module in which they
appear.
Public: Variables are available to all modules.
The Variant Data Type
Variant variables can hold any type of data described so far, and then
some.
Variables of this type take 16 bytes, in this format:
Bytes 0 and 1 hold an integer value that states which type of data is
stored in bytes 8 through 15.
Bytes 2 through 7 are unused (with only one exception, the Decimal
subtype), and in most cases not all the bytes in the second half of the
variable are used.
For example, if a Variant holds an Integer value, the first two bytes
contain the value 2-vbInteger, bytes 8 and 9 hold the actual 16-bit value,
and all other bytes are unused.
Variables Used Within a Procedure
You declare them with the Dim or Static keywords.
For example:
Dim intTemp As Integer
or
Static intPermanent As Integer
Variables Used Within a Module
You create module-level variables by declaring them with
the Private keyword in the Declarations section at the top
of the module.
Private intTemp As Integer
At the module level, there is no difference between
Private and Dim, but Private is preferred because it
readily contrasts with Public and makes your code easier
Variables Used by All Modules
To make a module-level variable available to other modules,
use the Public keyword to declare the variable.
The values in public variables are available to all procedures
in your application. Like all module-level variables,
Public variables are declared in the Declarations section at the
top of the module.
Public intTemp As Integer
Constants
The syntax for declaring a constant is:
[Public|Private] Const constantname[As type] = expression
A Const statement can represent a mathematical or date/time
quantity:
Const conPi = 3.14159
Public Const conMaxPlanets As Integer = 9
Const conReleaseDate = #1/1/95#
Const conCodeName = "Enigma"
You can place more than one constant declaration on a single line if
you separate them with commas:
Public Const conPi = 3.14, conMaxPlanets = 9
Data Types
By default, if you don't supply a data type, the variable is given the
Variant data type.
The Variant data type can represent many different data types in
different situations.
Declaring Variables with Data Types
You must use Private, Public, Dim or Static statement to declare it As
type.
Numeric Data Types
Visual Basic supplies several numeric data types
— Byte, Integer, Long
Single (single-precision floating point), Double (double-precision
floating point), and Currency.
The String Data Type
Private S As String
S = "Database"
You specify a fixed-length string with this syntax:
String * size
For example, to declare a string that is always 50 characters long, use
code like this:
Dim EmpName As String * 50
The Boolean Data Type
If you have a variable that will contain simple true/false
The default value of Boolean is False.
Dim blnRunning As Boolean
The Date Data Type
Date and time values can be contained both in the specific Date data type
and in Variant variables.
For Each...Next
Syntax :
For Each element In group
statements
Next element
IIf Function
Returns one of two parts, depending on the evaluation of an expression.
Syntax
IIf (expr, truepart, falsepart)
expr. Expression you want to evaluate.
truepart. Value or expression returned if expr is True.
falsepart. Value or expression returned if expr is False.
IIf always evaluates both truepart and falsepart, even though it returns
only one of them. Because of this, you should watch for undesirable side
effects.
For example, if evaluating falsepart results in a division by zero error, an
error occurs even if expr is True.
With Statement
Executes a series of statements on a single object or a user defined type.
Syntax
With object
[statements]
End With
Example:
With MyObject
.Height = 100 ' Same as MyObject.Height = 100.
.Caption = "Hello" ' Same as MyObject.Caption =
"Hello".
With .Font
.Color = Red ' Same as MyObject.Font.Color =
Red. .Bold = True ' Same as MyObject.Font.Bold =
True.