If the code I gave here is in UserForm1, you can access the value of it, and assign the value to it, from anywhere by refering to that Form:"
NOT quite correct.
Yes, you CAN refer to it (and assign it) -
without an error - from Userform2 with:
Msgbox Userform1.Lastname
Say Userform1 sets Lastname = "Jones"
Condition A: Userform1 is
still loaded.
U&serform2 instruction: Msgbox Userform1.Lastname
Result: "Jones"
Condition B: Userform1 is
NOT still loaded.
U&serform2 instruction: Msgbox Userform1.Lastname
Result: ""
The Public variable in Userform1 only retains its value
within scope. Once a object module is closed (and userforms are object modules), then all variables, Public or otherwise, are dead.
The interesting thing about this, is that even if you
do not use Userform1 - in other words, you never set Lastname - you can STILL execute:
Msgbox Userform1.Lastname
from Userform2
without an error.
The declaration of a Public variable in ANY module adds it to the listed available variables. You can see it in IntelliSense.
But again, it will ONLY have a value (
set in userform1!!!)as long as Userform1 is loaded.
You CAN set it in Userform2.
Public variable in Userform1 is Lastname.
Userform1 is NEVER loaded.
userform2 is loaded, and:
Userform1.Lastname = "Bob"
Msgbox Userform1.Lastname
will return Bob.
So yes, another userform can SET and use a Public variable declared in another userform module. But it can NOT return that variable value if it is set by userform1, and userform1 is not still loaded. Unloading userform1 sets the variable to Null.
For it to retain value (set by userform1) no matter what (to be truly Public IMO), it
must be declared in a standard module.
faq219-2884
Gerry
My paintings and sculpture