Public Date Variable Call

JohnLee

Registered User.
Local time
Today, 14:02
Joined
Mar 8, 2007
Messages
692
Hi,

I have created two public Date variables in my form as follows:

Code:
Public adhocStartDate As Date
Public adhocEndDate As Date

This two variables get their values from the following text boxes in my form:

txtStartDate
txtEndDate

I associated the values from the text boxs in the lost focus event of the txtEndDate text box as follows:

Code:
adhocStartDate = Me![txtStartDate].value
adhocEndDate = Me![txtEndDate].value

To check that the date entered in each text box was actually being assigned to the two public functions I put in a message box after the above code as follows:

Code:
MsgBox "adhoc Start Date : " & adhocStartDate
MsgBox "adhoc End Date : " & adhocEndDate

When I ran my process these two message boxes confirmed that the values from the texts boxes were indeed assigned to these two public variables.

However the problem lies in the module the should collect these values from these public variables.

In my module I have the following:

Code:
FromDate = adhocStartDate
ToDate = adhocEndDate

My module runs immeadiately after the End Date has been entered into the Form, so that is no time lapse or opening and closeing of form objects.

I put in message boxes to confirm that the public variable values were being assigned as required, but no values are coming through.

The message boxes were empty!

Code:
MsgBox "Adhoc Start Date : " & AdhocStartDate
MsgBox "Adhoc End Date : " & AdhocEndDate

So my question is can a public variable in a form object be passed to a module object, because if it can it's not working for me.

Any assistance pointers would be most gratefully received.

Regards
 
So my question is can a public variable in a form object be passed to a module object, because if it can it's not working for me.

I believe the way VBA variable/object scoping works in your specific case is that Public Modules are the most global thing to attach variables/objects to, and variables/objects on forms are only within the form while the form remains open.

So, I would suggest declaring the variables/objects you want to be truly global in the top of a VBA module, outside / before any function/subroutine declarations. Such will be truly application global variables.

As...

Forms may be opened / closed
Modules OTOH may not be opened / closed
 
Hi mdLueck,

Thanks for the suggestion I declared those public variables in the module and it worked a treat.

Regards

John
 

Users who are viewing this thread

Back
Top Bottom