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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Variables 1

Status
Not open for further replies.

LarryDeLaruelle

Technical User
May 19, 2000
1,055
US
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.
 
If the message is always going to be the same, use a public constant instead:

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
 
Thanks Jim. I should have thought to make it a constant.

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.
 
When you tried to assign (strTitle = "Medical Reports") the value to the variable, it bombed, because it has to be in either a Sub Routine or a Function on either a form or control Event, or in a module.

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:

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top