Scope Lifetime Modules Procedures Scope Local and Global
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 • Only your procedure can have full control of these little variables • AKA “procedure-level variables” • Dim int. Temp 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 int. Temp 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_int. General 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_int. Major. General as integer
Scope of Constants • …share constants among modules. • Constants are static and cannot be changed • Do the following in the Declarations • Public Const cstr. Who as String=“Jim”
Scope of Procedures • Limited use of procedures in the Module where they are located • Private Function Local. Star() • 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 form-related procs • Main location for event procs
Standard Modules • Best location for general utility procs • Only location for non. Form procs • Best location for constants and custom data structures: mod. Globals
Class Modules • Custom objects • Objects properties and methods • These modules will be covered later
More Standard Modules • Private • Publi
Modularity 301 For variables For Procs • • Private Public Dim Static Private Public Sub Function
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 (Msg. Box) vb. Answer=Msg. Box(vb. Msg. Box. Style: =vb. OKOnly, _ Prompt: =str. Question) • Calling syntax 2: Position args (Msg. Box) vb. Answer = Msg. Box(str. Question , vb. OKOnly)
Arguments - 3 • Coding syntax: use optional args • Optional args must be at end of arg list and must be variant Sub Test (Optional var. Times as Variant) If Is. Missing(var. Times) then var. Times = 10 Or Sub Test (Optional int. Times 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(By. Val height As Double, By. Ref 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 dbl. My. Area = Area (dblheight, (dblwidth))
Functions • Must be typed when coded • Function Msg(str. Msg) as vb. Msg. Box. Result • Useful as replacements for: – Calculated fields (normalization) – Global variables – Repetitive calls to Msg. Box, etc…
Assignment 2: Sign On Tasks • Get user security code (S or V) use VBA functions: Input. Box and Ucase • Adjust frm. Contribution to the user's security level use Allow. Edits, Allow. Deletions, cmd. Delete. 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 str. Level As String Dim bln. Not. OK As Boolean str. Level = Get. Security. Information ‘ get user's level Call Set. Up(str. Level, bln. Not. OK) ‘ setup form for level If bln. Not. OK Then Cancel = True End Sub ‘ cancel open
Pop Quiz 1. Public Sub Test() – Describe the possible scope of this procedure 2. mod. Utilities – Describe the scope of procedures in this module 3. Form_frm. Contribution – Describe the scope of procedures in this module 4. Public int. Counter as Integer – Describe the possible scope of this variable
- Slides: 29