Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variables not passing within project

Status
Not open for further replies.

jluft

Technical User
May 4, 2004
74
US
i have a variable in a project that is defined in one module. then, another module is called. for some reason the variable is not passing through to the new module when called. i have defined the variable as global...why would this be hapenning? thanks
 
A little more info would be helpful. What type of module is the variable declared in (standard code mod, Userform, ThisWorkbook, etc.)? What do you mean it's not passing through to another module? Do you get an error?

Regards,
Mike
 
mike,
it is declared in a standard code module (there are about 15-20 or so in the workbook)
i do not get an error...the strange this is that even though i am declaring the variables as public (or global, as i tried them), it will tell me that the variable is ambiguous unless i declare it in all of the individual modules in which it occurs. a very strange phenomenon, as this is not occurring universally but more on a sporadic basis

heres an example

public keyvar
sub main
call test1
call test2
end sub
------------
public keyvar
sub test1
if keyvar = "" then
keyvar = "hello"
end if

end sub
------------
public keyvar
sub test2
keyvar = "neither"
call test1
end sub

with what i am referring to, this program would end with keyvar having the value "hello" in sub1 and "neither" in sub2, instead of the result passing universally to both

hope this clarified
 
jluft,

Sorry, I'm still having some difficulty with what you are indicating when you say
with what i am referring to, this program would end with keyvar having the value "hello" in sub1 and "neither" in sub2, instead of the result passing universally to both
In the scenario you give, it appears that each sub is in a separate module and you've declared keyvar in each. The danger here is knowing which version of keyvar is being referenced at any given time. If you run procedure Main and check the value of keyvar, it will be blank (actually, whatever value it contains when Main runs). This is because within each module, an unqualified reference to keyvar will be to the one declared in that module. Try this modified version of your code to see what I mean:
Code:
'---------- Module1
Public KeyVar
Sub Main
Call ClearVars 'ensures all variables "blank"
Call Test1
Call Test2
MsgBox "Module1.KeyVar = " & Module1.KeyVar & vbLf & "Module2.KeyVar = " & Module2.KeyVar & vbLf & "Module3.KeyVar = " & Module3.KeyVar
End Sub

Sub ClearVars()
   KeyVar = ""
   Module2.KeyVar = ""
   Module3.KeyVar = ""
End Sub
'------------ Module2
Public KeyVar
Sub Test1
If KeyVar = "" Then
  KeyVar = "hello"
End If

End Sub
'------------ Module3
public KeyVar
Sub Test2
KeyVar = "neither"
Call Test1
End Sub

If you need to use a variable with the same name in multiple modules, you can do so by fully qualifying them as in the above code. If I'm not understanding your intent, let me know.

Regards,
Mike
 

Declare your public variable ONE TIME ONLY in ONE MODULE!!!

If you declare it in more than one module, you doe NOT have a truly global variable.

Skip,

[glasses] [red]Be advised:[/red]To be safe on the FOURTH, don't take a FIFTH on the THIRD, or...
You might not come FORTH on the FIFTH! [bomb][tongue]
 

Thank y'all, pilgrim! ;-)

Skip,

[glasses] [red]Be advised:[/red]To be safe on the FOURTH, don't take a FIFTH on the THIRD, or...
You might not come FORTH on the FIFTH! [bomb][tongue]
 
skip,
that was my initial plan..but for some reason it was calling some of the variables undeclared...i will try again
oddly, it is calling the variable ambiguous UNTIL i rename it..normally it would be hte other way around (ambiguity being caused by declaration in multiple locations)
thanks
 


I have Option Explicit. Works for me.

Skip,

[glasses] [red]Be advised:[/red]To be safe on the FOURTH, don't take a FIFTH on the THIRD, or...
You might not come FORTH on the FIFTH! [bomb][tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top