Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Public Variables

Public Variables

Public Variables

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?


Larry De Laruelle

RE: Public Variables

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".


MsgBox "Message Here", vbOKOnly, MSGTITLE

Jim Lunde
Custom Application Development

RE: Public Variables

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.

Larry De Laruelle

RE: Public Variables

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 Lunde
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close