can you use variables in different subs??

babypowdr

New member
Local time
Today, 07:41
Joined
Aug 27, 2003
Messages
5
If I define a variable 'DebitTot' in Sub SaveClick and use DMax to assign a value to it, can I then refer to that variable and value in a Sub AddNewClick?

Private Sub SaveClick
Dim DebitTot As String
DebitTot = DMax(Blah Blah Blah)
End Sub

Private Sub AddNewClick
PastDue = DebitTot
End Sub


Something to that affect?

Thanks,

Mac
 
Set

Global DebitTot As String

in a Public Module and remove

Dim DebitTot As String

from your code.
 
U can also declare the variable at the top of your form code, but only if u want to use the variable on that form only. Otherwise u have to declare it as a global variable.

Code:
Dim DebitTot As String
    
Private Sub SaveClick
    DebitTot = DMax(Blah Blah Blah)
End Sub

Private Sub AddNewClick
    PastDue = DebitTot
End Sub

Reason being, if u declare a variable in a sub procedure, then it only exists inside that sub procedure. So once the SaveClick procedure ends, the DebitTot variable doesn't exist anymore. If u declare the variable outside of a sub procedure, like above, then u can use that variable in any sub procedure on that form. And if u declare it as a global variable, then u can use it anywhere in your code (i.e. any form, report, module etc).
 

Users who are viewing this thread

Back
Top Bottom