By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,375 Members | 3,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,375 IT Pros & Developers. It's quick & easy.

declaring global variables

P: 3
I just read an older post that described declaring global variables. I tried this but I'm getting an error and the text stays red.
What am I doing wrong? This code is behind a form.

Option Compare Database

Global theDate As String

Function DetermineDate()
thedate = InputBox("Enter Volume Date YYYYMM")
mymonth = Right(thedate, 2)
appdate = MonthName(mymonth)
appyear = Left(thedate, 4)
End Function

Private Sub Command10_Click()
DoCmd.OpenReport "Report - Table7", acViewPreview
End Sub
Nov 15 '06 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 3,072
First you need to move the code into a Module, else it's scope will be limited to the form/report it's placed behind.

Option Compare Database
Option Explicit

Public theDate As String

Function GetDate() as String
GetDate = theDate
End Function

The above code in a module will allow you to use theDate even in a query like:

select * from tblX where VolumeDate = GetDate

You can use your from your code:
thedate = InputBox("Enter Volume Date YYYYMM: ")

This will fill the string and the GetDate() function will return the filled value.

Finally I wouls use Tools/Options to check the option that variables need to be defined. This will give the "Option Explicit" and warn you when using a not declared variable.

Getting the idea ?

Nov 15 '06 #2

Expert 100+
P: 1,418
You can't declare Global variables in From or report module!

Only in the part MODULES you create a module and you define your global variable!!!!

Also don't do the mistake to have the same name as global variable and as sub or function name as Nico suggested.. It' wonna't work!

If your declaration is placed at place your code seems correct!
Nov 18 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.