Assign value to variables

  • Thread starter Thread starter Alain
  • Start date Start date
A

Alain

Hi to all,

I would like to know on how to assign a value to a declared public variable
in a single module only, ( say a variable module) meaning without going thru
all other procedures to assign the same value to the same variable that are
stored in different module.
This is for taxes calculation purpose like the GST

TIA

Alain
 
If you want a value to be available to anywhere in the application as long as
the application is open, then my favorite way to do this is with a function
containing a static variable.
Put the function in a standard module so it is visible to any other module
in the application. To assign a value, call the function with a value:

GetClosingYear(Year(Date))

To retrieve the value, call it without an argument.

lngYearEnd = GetClosingYear

The way the function works is that if you pass it a value, it stores the
value in a Static variable replacing the current value of the variable. If
you do not pass it a value, it retains its current value. In either case, it
returns the current value of the static variable.

Public Function GetClosingYear(Optional varYear As Variant) As Long
Static lngClosingYear As Long

On Error GoTo GetClosingYear_Error

If Not IsMissing(varYear) Then
lngClosingYear = CLng(varYear)
End If
GetClosingYear = lngClosingYear

GetClosingYear_Exit:

On Error Resume Next
Exit Function

GetClosingYear_Error:

MsgBox "Error " & Err.Number & " (" & Err.DESCRIPTION & _
") in procedure GetClosingYear of Module modLoadMonth"
GoTo GetClosingYear_Exit
End Function
 
Back
Top