Global Variables and Static

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi everyone

I want to know how I can use global variables - so that a sub in one module can communicate with a sub in a different module. I suspect that the word "static" might have the answer.

Currently I'm using a class called GlobalStuff to hold my global variable - name. I then use class2 to set name to a string. In class3 I want to be able to extract the value class2 put into GlobalStuff - but when I reference GlobalStuff I have to create a new instance, and the variable is reset to empty

The point of this is to build a control to close a report - my client has an aversion to the "x" button on the window for some reason

Any help gratefully received...
 
Alister,

If you're using a class to hold this information, then all you need is a
global object variable, which will be visible and accessible to the entire
project.

But if all you want to do is close a report, you don't need a class at all.
Just create a public procedure, like so:
Public Sub CloseReport(sRptName As String)
DoCmd.Close acReport, sRptName
End Sub

But why bother? I must be missing the point here!

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Alister said:
Hi everyone!

I want to know how I can use global variables - so that a sub in one
module can communicate with a sub in a different module. I suspect that the
word "static" might have the answer.
Currently I'm using a class called GlobalStuff to hold my global
variable - name. I then use class2 to set name to a string. In class3 I want
to be able to extract the value class2 put into GlobalStuff - but when I
reference GlobalStuff I have to create a new instance, and the variable is
reset to empty.
The point of this is to build a control to close a report - my client has
an aversion to the "x" button on the window for some reason.
 
you can just declare a variable with public in normal module - then it will
be accessible from any sub or class

public MyVar as string

--
Alex Dybenko (MVP)
http://Alex.Dybenko.com
http://www.PointLtd.com



Alister said:
Hi everyone!

I want to know how I can use global variables - so that a sub in one
module can communicate with a sub in a different module. I suspect that the
word "static" might have the answer.
Currently I'm using a class called GlobalStuff to hold my global
variable - name. I then use class2 to set name to a string. In class3 I want
to be able to extract the value class2 put into GlobalStuff - but when I
reference GlobalStuff I have to create a new instance, and the variable is
reset to empty.
The point of this is to build a control to close a report - my client has
an aversion to the "x" button on the window for some reason.
 
Hey Guy

Thanks for the help

The reason for this is that the client doesn't feel comfortable with that little "x" button at the top right hand corner of the window. Don't ask me why.
 
Alister,

I must still be missing something here. Nevertheless, what you need to do is
declare a Public variable (in a module) of the GlobalStuff type. That's all!
Public GS As GlobalStuff

....then you can access the class from anywhere, without having to
re-instantiate it. Of course, if your application throws an error, the
variable will be reset anyway.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Alister said:
Hey Guys

Thanks for the help.

The reason for this is that the client doesn't feel comfortable with that
little "x" button at the top right hand corner of the window. Don't ask me
why.
 
Back
Top