Download presentation
Presentation is loading. Please wait.
Published byColleen Payne Modified over 9 years ago
1
Scope Lifetime Modules Procedures
2
Scope? Where can your variables be seen? Where used? Where abused (reseting the value)? Local and Global = Private and Public
3
The usual idea of scope …limit variables to their own procedures Only your procedure can have full control of these little variables AKA “procedure-level variables” Dim intTemp as integer each time the procedure is run the Dim resets the contents of the variables
4
The main idea of lifetime …carry over values of variables to the next time the procedure is called. Static intTemp as integer Keep a counter going… …or remember the last thing you did for the calling program
5
Another idea of scope …share variables among your procedures in your module. Scope extends outside Procedure but still limited to within a Module AKA “module-level variables” Do the following in the Declarations Private m_intGeneral as integer
6
Yet another idea of scope …share variables among modules in your Application Known as Public variables or globals Dangerous in large projects Use Functions instead Do the following in the Declarations Public g_intMajorGeneral as integer
7
Scope of Constants …share constants among modules. Constants are static and cannot be changed Do the following in the Declarations Public Const cstrWho as String=“Jim”
8
Scope of Procedures Limited use of procedures in the Module where they are located Private Function LocalStar() Unlimited use of general utility procedures in the Application (standard modules) Public Function Popular()
9
Modules Where do you put VBA programs?
10
Module Choices Form Class Modules Standard Modules Class Modules
11
Form Class Modules Automatically created for you when you have wizard-written controls Best location for form-related procs Main location for event procs
12
Standard Modules Best location for general utility procs Only location for nonForm procs Best location for constants and custom data structures: modGlobals
13
Class Modules Custom objects Objects properties and methods These modules will be covered later
14
More Standard Modules Private Publi
15
Modularity 301 For variables Private Public Dim Static For Procs Private Public Sub Function
16
Warnings Duplicate proc names can be OK Duplicate module names can be OK Duplicate variable names can be OK Global variables are rarely Ok (a global would be declared as Public in a standard module's declaration section)
17
Procedures AKA PROCS
18
Subs Great for processes! Ok for return values, but not great Know where to file them Example: Assignment 2 (Security)
19
Functions Great for return values! Ok for processes, but not great Know where to file them Example: Assignment 2 (Security)
20
Arguments - 1 Not required, but preferred for communication between the caller and the coded procedure. Without args you have only public variables for communication, a weak practice. Use named arg syntax or position syntax for args
21
Arguments - 2 Calling syntax 1: Named args (MsgBox) vbAnswer=MsgBox(vbMsgBoxStyle:=vbOKOnly,_ Prompt:=strQuestion) Calling syntax 2: Position args (MsgBox) vbAnswer = MsgBox(strQuestion,vbOKOnly)
22
Arguments - 3 Coding syntax: use optional args Optional args must be at end of arg list and must be variant Sub Test (Optional varTimes as Variant) If IsMissing(varTimes) then varTimes = 10 Or Sub Test (Optional intTimes as Integer = 10) …etc
23
Arguments - 4 Coding syntax: args as values or as addresses Args as values cannot be changed by proc Args as addresses can be changed by proc Public Function Area(ByVal height As Double, ByRef width As Double) As Double Area = height * width End Function
24
Arguments - 5 Calling syntax: forcing args as (values) Args as values cannot be changed by proc dblMyArea = Area (dblheight, (dblwidth))
25
Functions Must be typed when coded Function Msg(strMsg) as vbMsgBoxResult Useful as replacements for: –Calculated fields (normalization) –Global variables –Repetitive calls to MsgBox, etc…
26
Assignment 2: Sign On Tasks Get user security code (S or V) use VBA functions: InputBox and Ucase Adjust frmContribution to the user's security level use AllowEdits, AllowDeletions, cmdDelete.Enabled
27
Assignment 2 Pseudo Code Loop while security input Ask for security password input If (Supervisor or Volunteer or Cancel) exit loop Ask if user wants to try again If not to try again abort form open End loop Case Supervisor Allow data entry, record deletions, edits to stored records Case Volunteer Allow data entry Case Cancel Abort form open
28
Assignment 2 High-level Code Private Sub Form_Open(Cancel As Integer) Dim strLevel As String Dim blnNotOK As Boolean strLevel = GetSecurityInformation ‘ get user's level Call SetUp(strLevel, blnNotOK) ‘ setup form for level If blnNotOK Then Cancel = True ‘ cancel open End Sub
29
Pop Quiz 1.Public Sub Test() –Describe the possible scope of this procedure 2.modUtilities –Describe the scope of procedures in this module 3.Form_frmContribution –Describe the scope of procedures in this module 4.Public intCounter as Integer –Describe the possible scope of this variable
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.