View Full Version : preserve variable value
saban
04-11-2006, 12:44 AM
I set :
private sub command_button click()
dim doc as document
set doc = documents.add
end sub
How can I preserve variable doc-value so that in other subs will be recognizable
(lets say doc is nov document1 and when sub ends it should stay doc = document1)
Thnx
Killian
04-11-2006, 02:15 AM
This is an issue of "scope."
Scope describes the "visibility" of a variable. If you make your variable declaration inside a routine (as you have in this case) it is only available inside that routine.
You can raise the scope to module level by making the declaration at the start of the module, outside any routines. It will now be visible to all the module's procedures and functionsOption Explicit
Dim doc As Document
Private Sub CommandButton1_Click()
Set doc = Documents.Add
End Sub
Private Sub CommandButton2_Click()
If doc Is Nothing Then
MsgBox "Variable 'doc' not set" & vbLf & _
"Please click button 1 first"
Else
MsgBox "Variable 'doc' set to document: " & doc.Name
End If
End SubAn important point to realise is that in a standard module, such a variable will be available all the time the project is loaded. In a userform module, the variable is disposed of with the userform when it is unloaded.
This illustrates a difference in the "lifetime" of a variable.
An further level of scope is a "global" variable. If you wanted to unload the userform but still have "doc" available for use, you could declare it in a standard module as Public. It would then be visible to all functions and procedures in the projectPublic doc As Document
A consequence of increasing the scope of a variable (esp. to global) is that you need to manage when you change as part of your program design, since it's increased visibility means it can easily be changed by a re-used procudure or function at any point.
You'll find more info and examples of managing scope and lifetime in VBA help and numerous articles on the 'net. It's a crucial concept in programming, so well worth taking the time to research and understand.
Hope that helps...
fumei
04-11-2006, 05:40 AM
Very nice K. Scope IS a crucial concept, and Saban, it is therefore crucial to understand it, and use it properly. Help is very good, but I also suggest doing actual tests for yourself.
Make variables that are in scope within a procedure, within a module, and globally. Make tests so you can see where a variable goes out of scope...which is the thing that is so crucial!
And NEVER forget to destroy object variables that are not being used. Even if you going to create another one just like it. If THAT variable is not being used anymore, destroy it.
So your set doc = documents.add should always, somewhere, have a set doc = nothing. This is, in fact, a good part of testing for scope.
saban
04-11-2006, 06:22 AM
Thnx guys
mdmackillop
04-14-2006, 03:26 AM
Very nice K.
Agreed :thumb
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.