By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,669 Members | 2,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,669 IT Pros & Developers. It's quick & easy.

public variables

P: n/a
Hi How can i use public variables. These variables i wand to use in
different places on a form (like a button) and are neccessery to write the
type of variable like:

Public VAR1, var2 as integer
or

Public VAR1, var2
Thanks
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
It is better to use
Public VAR1 as integer, var2 as integer

Madhivanan

Nov 13 '05 #2

P: n/a
Parintas Themis STE Kardias wrote:
Hi How can i use public variables. These variables i wand to use in
different places on a form (like a button) and are neccessery to
write the type of variable like:

Public VAR1, var2 as integer
or

Public VAR1, var2
Thanks


Acces VBA does not support the comma separated declaration the same way that
other environments do. *Each and every* variable has to be given a type or
it will end up as a Variant.

Ex: (only last variable below will be an Integer)

Public Var1, Var2, Var3 as Integer

Instead you should use...

Public Var1 as Integer
Public Var2 as Integer
Public Var3 as Integer

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 13 '05 #3

P: n/a
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
dl*********@hotmail.com
http://amazecreations.com/datafast/

"Madhivanan" <ma************@gmail.com> wrote ...
It is better to use
Public VAR1 as integer, var2 as integer

Madhivanan

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.