Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Variables..how does this go?

Status
Not open for further replies.

ChrisCalvert

Technical User
Mar 18, 2002
231
US
I have a public function that I run, and I want it to increment a variable. I need a form module to use a mesagebox to display this value. I am not familliar with
using public variables and was wondering if anyone could just show me where to declare what. Do I have to pass the varaible to the function? Here is what my stuff looks like now:

The Form Module
---------------------------------------------

Option Compare Database
Public intBadTracker As Integer

Private Sub Command0_Click()
'code
MsgBox intBadTracker

End Sub
---------------------------------------------




The Module with the Function
------------------------------------

Option Compare Database

Public Function fctnCounter(Optional Var1, Var2 As String)

'various code
intBadCounter = intBadCounter +1
'other code

End Function
--------------------------------------
 
Hi!

To reference a public variable from a form you need to reference as a property of the form:

Forms.YourForm.intBadCounter

That said, I am not sure that you can increment it when you call it this way. If not, you can set up a public procedure in your form to increment it and call the procedure from the module when you want to increment the variable. Alternatively, you can make it a global variable which can be called from anywhere, or just store the variable in a text box on the form which you reference and change from anywhere.

hth
Jeff Bridgham
bridgham@purdue.edu
 
Jeff:

How would the global variable go? Would I just replace Public with Global, or do I have to declare that elsewhere?

-chris
 
Hi Chris!

A global variable is declared in a module with the keyword Public.

hth
Jeff Bridgham
bridgham@purdue.edu
 
I would suggest making your incrementer function generic since that is a handy little operation to have. Pass the value to increment so it can stand alone. Create a function something like the following in a standard module:

Public MyIncrementer(varNumber As Variant, _
Optional varIncrement As Variant = 1) As Variant
If Isnumeric(varNumber) Then
If Isnumeric(varIncrement) Then
MyIncrementer = varNumber + varIncrement
Else
'maybe a msgbox here if error or increment by 1
MyIncrementer = varNumber
End If
Else
'maybe a msgbox here if error or increment by 1
MyIncrementer = varNumber
End If
End Function

I made the arguments variants to allow for any kind of numeric data since Access doesn't handle overloads easily.

Once you have your function defined in a standard module, you can increment from anywhere by referring to it like so in either your form or other module code.

intX = MyIncrementer(intX)
lngX = MyIncrementer(lngX, -1)
dblX = MyIncrementer(dblX, 1.6532)

Good Luck!
 
Sorry,

It should be Public Function MyIncrementer(etc,

You declare a public or module variable in the declarations area of a module or form at the top with the Options.

Public intX As Integer - This is a public or global variable and is visible from anywhere within the code. It is also visible to other databases via proper qualifications to the database.

Private intX As Integer - This is a module or private variable and is visible from anywhere within the code but is not visible to outside programs.

Hope this helps and Good Luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top