Assign value to variables



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




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:


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


On Error Resume Next
Exit Function


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question