Declare global variable and assign value globally (for all functions on a form)

Posted on 2013-10-29
Last Modified: 2013-10-30
I have a form that sends emails when different fields are triggered. I want to assign an address list to a string by using global variables (so I can edit the list @ one location).

I tried adding this just below the "options compare database" section but it is not recognizing the address list when the email is triggered. I know the email list is formatted properly because I added it directly to the .to  section and it worked perfectly. How do I declare this list globally so i can use the EmailsAddressList string anywhere in the code?

Public EmailsAddressList As String
EmailsAddressList = ";"

Thanks in advance!
Question by:filtrationproducts
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39608826
Several ways to do this.

I prefer to use TempVars.  I believe these were new with 2007.  You can add or change TempVars at any time in your application using code similar to:

tempvars.Add "EmailTo", ";"

And can then recall those values using:


Another method I used to use (before TempVars), was to create a function:

Public Function fnEmailTo(Optional EmailTo as variant = Null) as String

    Static myEmailTo as variant

    if isnull(EmailTo) = False then myEmailTo = EmailTo

    fnEmailTo = NZ(myEmailTo, "")

End Function

This method allowed me to set the value of the function with code like:

fnEmailTo ";"

and to recall the value with


When you pass the value to the function, it stores that value in a static variable.  When you call the function without a value, it returns the value stored in the static variable.
LVL 31

Expert Comment

ID: 39609138
It isn't easy to edit a global (or any) variable.  It might work better to save the emails list to a field in a table, where you could easily edit them as needed.  I use a little one-row table called tblInfo for such data.

Author Comment

ID: 39609223

I have Access 2007 but this file is an .mdb. Will tempvar's work with this file?

The reason I ask is I added the following code below the "Option Compare Database" section but I am receiving an error that says "Invalid Outside Procedure".

TempVars.Add "EmailTo", ";"

Open in new window

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39609255
If you are running A2007, then TempVars are available.

You do not declare a tempvar, like you would a global variable.

The TempVars collection already exists.  All you have to do is set that value somewhere, I would do it in a form or standard code module.  If this is going to be global, then I would generally put that line of code in the Open even of my startup form.

But you could also have an options form, or something like that, that uses a table (like Helen recommended) to allow you to edit this value.
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39609260
Another neat thing about tempvars, is that you can set the value in the immediate window using the same syntax, which allows you to easily test forms, queries, reports, and modules that use the value.

Author Comment

ID: 39609272
I like the idea of using a table to add the contacts. If I put the email addresses in a table, how would I call that table data and format it to send to those using the Outlook.Application?

Currently I just dim a string and call it using

.To = whatevermystringis 

Open in new window

LVL 47

Accepted Solution

Dale Fye (Access MVP) earned 150 total points
ID: 39609292
With tempvars, you would use your splash (or menu) form to set the value:

Private Sub Form_Load

    TempVars.Add "EmailTo", ";"

End Sub

Then in your email code you would just use:

.To = TempVars("EmailTo")

To store that in a table, you would either need to have a form, linked to a table (Helen indicates she uses tblInfo), and have a textbox for editing those values.  You would obviously need a way to open that form, but to use those values, you could do something like:

.To = DLOOKUP("EmailTo", "tblInfo")

Author Closing Comment

ID: 39609504
Using the tempvars function worked to solve my problem as described in my original question.

Thanks for your help!
LVL 31

Expert Comment

ID: 39612531
If the data is stored in a table, you can retrieve it using a function like the following:

Function GetTemplatePath() As String
'Created by Helen Feddema 17-Nov-2005
'Modified by Helen Feddema 24-Jul-2006

On Error GoTo ErrorHandler

   Set dbs = CurrentDb
   Set rst = dbs.OpenRecordset("tblInfo", dbOpenDynaset)
   GetTemplatePath = Nz(rst![TemplatePath]) & "\"
   Set rst = Nothing
   Set dbs = Nothing
   Exit Function

   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit

End Function

Open in new window


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Google drive Permission Error 43 47
Union & Crosstab qrys 101! 6 56
WMIC Output Formatting 1 46
MS Access VBA Control Visibility Not Working Properly 7 22
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question