290 likes | 435 Views
Scope Lifetime Modules Procedures. Scope?. Local and Global = Private and Public. Where can your variables be seen? Where used? Where abused (reseting the value)?. The usual idea of scope. …limit variables to their own procedures
E N D
Scope? Local and Global = Private and Public • Where can your variables be seen? • Where used? • Where abused (reseting the value)?
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
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
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
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
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”
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()
Modules Where do you put VBA programs?
Module Choices • Form Class Modules • Standard Modules • Class Modules
Form Class Modules • Automatically created for you when you have wizard-written controls • Best location for form-related procs • Main location for event procs
Standard Modules • Best location for general utility procs • Only location for nonForm procs • Best location for constants and custom data structures: modGlobals
Class Modules • Custom objects • Objects properties and methods • These modules will be covered later
More Standard Modules • Private • Publi
For variables Private Public Dim Static For Procs Private Public Sub Function Modularity 301
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)
Procedures AKA PROCS
Subs • Great for processes! • Ok for return values, but not great • Know where to file them • Example: Assignment 2 (Security)
Functions • Great for return values! • Ok for processes, but not great • Know where to file them • Example: Assignment 2 (Security)
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
Arguments - 2 • Calling syntax 1: Named args (MsgBox) vbAnswer=MsgBox(vbMsgBoxStyle:=vbOKOnly,_ Prompt:=strQuestion) • Calling syntax 2: Position args (MsgBox) vbAnswer = MsgBox(strQuestion ,vbOKOnly)
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
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
Arguments - 5 • Calling syntax: forcing args as (values) • Args as values cannot be changed by proc dblMyArea = Area (dblheight, (dblwidth))
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…
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
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
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
Pop Quiz • Public Sub Test() • Describe the possible scope of this procedure • modUtilities • Describe the scope of procedures in this module • Form_frmContribution • Describe the scope of procedures in this module • Public intCounter as Integer • Describe the possible scope of this variable