Public Variables
Public Variables
(OP)
I am trying to set up a user form for report production and want a public variable that will be the Message Box Title for all displayed error messages. I have declared the variable as:
Option Compare Database
Option Explicit
Public strTitle As String
strTitle = "Medical Reports"
This seems to work fine until I add an On Open or On Load event (to set the enabled/visible properties of various objects). As soon as I do and try to run the form I get a message:
"The expression On Load you entered as the event property setting produced the following error: Invalid outside procedure."
When I click on OK it still loads the form.
The curious thing is, I haven't put any code in the On Load event, just added it from the Properties Event tab.
Any idea what I am doing wrong here?
Thanks.
Option Compare Database
Option Explicit
Public strTitle As String
strTitle = "Medical Reports"
This seems to work fine until I add an On Open or On Load event (to set the enabled/visible properties of various objects). As soon as I do and try to run the form I get a message:
"The expression On Load you entered as the event property setting produced the following error: Invalid outside procedure."
When I click on OK it still loads the form.
The curious thing is, I haven't put any code in the On Load event, just added it from the Properties Event tab.
Any idea what I am doing wrong here?
Thanks.
Larry De Laruelle
larry1de@yahoo.com
RE: Public Variables
Public Const MSGTITLE as String = "Medical Reports"
I always capitalize all my constants, but that is personal preference. I also have a module called modPublics, that is only for public constants, and variables. This way I always know where to find them, but again, that's just the way I do it.
Now you can refer to MSGTITLE anywhere in your application, and it is the same as using the string "Medical Reports".
Example:
MsgBox "Message Here", vbOKOnly, MSGTITLE
Jim Lunde
compugeeks@hotmail.com
CompuGEEKS
Custom Application Development
RE: Public Variables
Was the Public declaration causing the error message to be displayed after I added the On Load event? That still has me puzzled.
Thanks again.
Larry De Laruelle
larry1de@yahoo.com
RE: Public Variables
You can't simply put strTitle = "Medical Reports" in a module, because Access will not assign the value until it is part of an action (sub or function). You can declare publicly, but you can't set publicly outside of a sub of function.
So you were getting the error because Access could not assign the value the way you had it set up. What you could have done, is on the "On Open" event of your form, you could have set the variable, and then you wouldn't have had a problem. However, a constant is better in this case, as you don't have to keep setting the variable every time you open a form, or perform an action.
Jim Lunde
compugeeks@hotmail.com
CompuGEEKS
Custom Application Development
RE: Public Variables
You're the Man!
Thanks for the help, it works great and I've learned (I hope) and new trick.
By the way, I passed your name on to another poster who has a problem similar to the one you helped me with earlier (creating SQL/Queries within code).
Is this the right protocol for this or should I have let you know first? Still kind of new to these forums.
Larry De Laruelle
larry1de@yahoo.com
RE: Public Variables
Jim Lunde
compugeeks@hotmail.com
CompuGEEKS
Custom Application Development