Standard Modules
Standard Modules
Up until now we have been learning the basics of VB programming and you should have a good understanding of how to get around and create programs in VB.
We will now move into more complex programming techniques.
A Standard Module is one of those techniques which will be used greatly as you work on larger projects.
Standard Modules
A standard module is a separate container in a program that contains global, or public, variables and Function and Sub procedures.
As you write larger programs, youre likely to have several forms and event procedures that use some of the same variables and routines.
By default, variables are local to the procedure in which they are created.
Standard Modules
Variables can be changed or read only in the event procedure in which they were created.
Event procedures are also local to the form in which they were created.
For example, you cant call the cmdQuit_Click event procedure from Form2 if the event was written in Form1.
Standard Modules
To share variables and procedures with all the forms and event procedures in a project, you need to declare them in one or more standard modules.
A standard module, or code module, is a special file that has the filename extension .bas and that contains variables and procedures that can be used anywhere in the program.
Standard Modules
Just like forms, Standard Modules are listed separately in the Project window.
It can be saved to disk by using the Save Module1 As command on the File menu.
Unlike forms, Standard Modules contain no objects or property settings--only code that can be displayed and edited.
Creating a Standard Module
To create a new Standard Module, click on the Add Form button down arrow on the toolbar and click Module.
When you create a new Standard Module, it appears immediately in the Code window.
The first Module is name Module1, but you can change that.
Creating a Standard Module
To change the name of the module, click on the name property (the only property visible) and change the name.
Lets change the name to modVariables
Lets now save the Module by clicking on Save modVariables as from the File menu.
Working With Public Variables
Declaring a global, or public, variable in a standard module is simple--you type the keyword Public followed by the variable name.
After you declare the variable, you can read it, change it, or display it in any procedure in your program.
Working With Public Variables
For example, the program statement Public RunningTotal would declare a public variable named RunningTotal in a standard module.
By default, public variables are declared as variant types in modules, but you can alter that.
Working With Public Variables
For example: Public LastName As String woul d declare a public string variable named LastName in your program.
Lets modify our Lotto picker to add a Public variable that counts how many picks we have made.
Working With Public Variables
If we had declared our variable locally in the Command1_Click event procedure, the variable would have reset each time.
Using a public variable in a standard module lets us avoid hitting reset.
Creating General Purpose Procedures
In addition to containing public variables, standard modules can contain general-purpose procedures that can be called from anywhere in the program.
A general -purpose procedure is like a built-in VB statement and function.
They are called by name, they can receive arguments, and each performs a specific task.
General Purpose Procedures
For example, imagine that a program has 3 mechanisms for printing a bit map:
A menu command named Print
A Print toolbar button
And a drag-and-drop printer icon
Instead of recreating that procedure 3 times, we can create one General-purpose procedure in a standard module.
General Purpose Procedures
General Purpose procedures save us time, make programs smaller and easier to handle, and make event procedures easier to read.
There are three types of general-purpose procedures in a standard module:
Function procedures
Sub Procedures
Property procedures
Function Procedures
Function procedures are called by name from event procedures or other procedures.
They can also receive arguments, and they always return a value in the function name.
They are typically used for calculations.
Sub Procedures
Sub procedures are called by name from event procedures or other procedures.
They can also receive arguments, and they can be used to perform tasks in the Unlike functions, however, Subs do not return values associated with their names.
Sub procedures are typically used to receive or process input, display output, or set properties. procedure and to return values.
Property Procedures
Property procedures are used to create and manipulate user-defined properties in a program.
This is a useful feature that lets you customize existing VB controls and extend the VB language.
You need to be quite advanced to use this procedure.
Benefits of General Purpose Procedures
The eliminate repeated lines.
Make programs easier to read.
Simplify program development.
Can be reused in other programs.
Extend the VB language
Writing Function Procedures
A Function procedure is a group of statement located between a Function statement and an End Function statement in a standard module.
The statements do the meaningful work--processing text, handling input, or calculating a numeric value.
Writing Function Procedures
You execute, or call, a function in a program by placing the function name in a program statement along with any required arguments.
In other words, a Function procedure works just like using a built-in function such as Time, Int, or Str.
Function Syntax
The basic syntax of a function is as follows:
Function FunctionName([arguments]) [As Type]
function statements
End Function
Function Syntax
The following example computes the VAT tax and adds an EU tax.
Function TotalTax(Cost)
VatTax=Cost*.22
EuTax=Cost*.10
TotalTax=VatTax+EuTax
End Function
Calling a Function Procedure
To call the TotalTax function in an event procedure, you would use a statement similar to the following:
lblTaxes.Caption=TotalTax(500)
This statement computes the total taxes required for a $500 item and then assigns the result to the Caption property of the lblTaxes object.
Calling a Function Procedure
The TotalTax function can also take a variable as an argument, as shown below:
TotalCost=SalesPrice+TotalTax(SlaesPrice)
this line uses the TotalTax function to determine the taxes for the number in the SalesPrice variable and then adds them to SalesPrice to get the total cost of an item.
Writing Sub Procedures
A sub procedure is similar to a Function procedure, except that a Sub doesnt return a value associated with its name.
Subs are typically used to get input from the user, display or print information, or manipulate several properties associated with a condition.
Subs are also used to process and return several variables during a procedure call.
Writing Sub Procedures
Most functions can return only one value, but Sub procedures can return many.
Sub Procedure Syntax
Sub ProcedureName([arguments])
procedure statements
End Sub
By default, Sub procedures declared in a standard module are public, so they can be called by any event procedure.