dbTalk Databases Forums  

public variables

comp.databases.ms-access comp.databases.ms-access


Discuss public variables in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Parintas Themis STE Kardias
 
Posts: n/a

Default public variables - 09-16-2005 , 07:30 AM






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



Reply With Quote
  #2  
Old   
Madhivanan
 
Posts: n/a

Default Re: public variables - 09-16-2005 , 07:35 AM






It is better to use
Public VAR1 as integer, var2 as integer

Madhivanan


Reply With Quote
  #3  
Old   
Rick Brandt
 
Posts: n/a

Default Re: public variables - 09-16-2005 , 08:42 AM



Parintas Themis STE Kardias wrote:
Quote:
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




Reply With Quote
  #4  
Old   
Danny J. Lesandrini
 
Posts: n/a

Default Re: public variables - 09-16-2005 , 09:12 AM



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




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.