Reference Code in another Module

JohnLee

Registered User.
Local time
Today, 08:50
Joined
Mar 8, 2007
Messages
692
Hi,

I have a number of Modules in my database and I want to reference some code in my first module from my last module and I'm having a problem identifying how I can reference the code I need in my first module.

I want to obtain the value of the this code to carryout a calculation in my last module.

So in my module called Process_01 I have this following slice of code:

Code:
[FONT=Arial]StartTime = Format(Now(), "hh:mm:ss") [/FONT]
[FONT=Arial][/FONT]

and in my module called Process_04 and I want to get the value of StartTime so that I can perform my calculation, so in this module I have the following code:

Code:
[FONT=Arial]    EndTime = Format(Now(), "hh:mm:ss")               [/FONT]
[FONT=Arial]    [/FONT]
[FONT=Arial]    TimeTaken = Format(EndTime - StartTime, "hh:mm:ss") [/FONT]

So in order for the TimeTaken to work I need to get the value of the StartTime from my module called Process_01.

Any help/pointers in the right direction would be most appreciated.

Regards


John
 
Why not declare the variables as 'global'?? that way they can use across different functions/subs
 
Hi,

Thanks for your response, this may sound like I'm being a bit dense, but I've never done anything to do with Global variables, how do you declare a Global variable, how do you put the value into it and get it out of it?

regards

John
 
Hello John, Global variables are nothing but variables that are declared in modules, as modules can be referenced across all forms so can the variables declared in Modules..
Not inside Module's functions, as in :
Code:
Public Sub someSub()
Dim strVar As String
Debug.Print "This strVar can be used only inside this Sub"
End Sub
But declare it as :
Code:
Option Compare Database
Dim globalStrVar As String
If you have declared that way you can use it anywhere..
Code:
Private Sub Form_Current()
Debug.Print "Once the value is initialised, you can use it : " & globalStrVar
End Sub
Hope that explains stuff..
 
Hi,

Does it have to have it's own module to be declared for global use i.e.

Code:
Option Compare Database
Dim StartTime As Date

I tried declaring it before my code in my Process_01 module and ran my code for both Process_01 and Process_04 but the value did not come through! is there something more I should be doing, do I need to also declare it as a global in the Process_04 module before the code in there?

Regards

John
 
No it can be on any of the Module..do not need to be declared in a separate module..
 
Hi,

Thanks very much for your help, I've followed your information and got it working now, I had declared it too many times, which was why the value wasn't coming through, it was taking the value from the latest declared variable. Removed all those not needed and it's working.

Thanks once again.

John
 

Users who are viewing this thread

Back
Top Bottom