It's better to use functions or Property Get and Property Let methods.
Here's the approach I've taken to using with respect to public variables:
' Create new module named bas_Fetch
' Create a private variable for each attribute you expose
Private m_lngProjectID As Long
' Create two functions to Set and Get the value
' (functions, not subs, so they may be called from control properties)
Public Function SetProjectID(ByVal lngValue As Long) As Boolean
On Error Resume Next
' first, save the value to the registry for later reference
SaveSetting "My App","Last Values", "ProjectID", lngValue
m_lngProjectID = lngValue
End Function
Public Function GetProjectID() As Long
On Error Resume Next
If m_lngProjectID = 0 Then
' If it hasn't been set, go look up last value I saved to Registry.
m_lngProjectID = GetSetting("My App","Last Values", "ProjectID", -1)
End If
GetProjectID = m_lngProjectID
End Function
Now, from my query I can use the function GetProjectID() to return the
value, provided I have previously set the value. However, either way, the
function returns a value and the query runs, even if it returns 0 records
because ProjectID is set to -1 when no other value is provided.
This is supperior to putting a form-control reference in the query criteria,
like this ...
Forms!frmMain!cboProjectID
That call will fail if the form is not open ... and not gracefully so. I put
a SetProjectID(Nz(Me.cboProjectID,-1)) call in the AfterUpdate event of
the combo box (and in the form OnCurrent() event) so that my "public variable"
remains synched with the current value.
You can get really tricky with this, once you start using it. For example, if
I want to provide an "<All Projects>" option for your combo box, you change
the type of lngProjectID to strProjectID and set the row source of the query
to something like this ...
SELECT '*' As ProjectID, '<All Projects>' As ProjectName FROM usysVersionClient
UNION SELECT ProjectID, ProjectName FROM tblProjects ORDER BY ProjectName
This works for me because I have a table, usysVersionClient, which has a single
row with the version number, but you could substitute MsysObjects and it would
work the same. The UNION suppresses duplicates, so the * appears only once.
Then you have to change the return value of GetProjectID to string and the
type of the argument in SetProjectID to string as well, and the query criteria
now looks like this ...
LIKE GetProjectID()
Since only Project 76543 is LIKE 76543, only one record is returned, but
since ALL projects are LIKE *, that option returns them all.
--
Danny J. Lesandrini
dlesandrini (AT) hotmail (DOT) com
http://amazecreations.com/datafast/
"Madhivanan" <madhivanan2001 (AT) gmail (DOT) com> wrote ...
Quote:
It is better to use
Public VAR1 as integer, var2 as integer
Madhivanan |