Declaring constants/variables

D

Dragon

This is a REAL beginner question. I have a series of userforms for which I
want to use the same variables/constants (I am not sure the difference
between those two).

What I want is be able to use the following in all of my userforms.
Therefore, I tried placing them in my Module (thinking that was a "global"
place for them to be accessible by all, and I received errors.

Public temps As Templates
Temps = application.templates

I tried a series of combination, but it's not happy. Any help/explanation
you can provide would be much appreciated.

Thanks.
 
J

JGM

Hi Dragon,

You are trying to declare an object (a template), not a variable.

Try:
Public temps As Templates
Set Temps = Application.Templates
The key word being "Set".

By the way, a variable varies, and a constant does not... that is the
difference!
Seriously...
Use "Dim" to declare variable and "Const" to decalre constant (unless they
are Global). When you decalre a variable, you associate a type, for
constant, you assign a value....
Something ike:

'_______________________________________
Public MyVar As Long
Public Const MyCons As String = "My Text"
'_______________________________________
Sub Test()

Dim AnotherVar As String
Const AnotherCons As Long = 108

MyVar = 34
AnotherVar = "Hello there!"

End Sub
'_______________________________________

HTH
Cheers!
 
D

Dragon

Jean-Guy,

I tried your suggestion with the SET and I get "Invalid - Outside
Procedure". Again, I want to put this in the global Module for all the
userforms to be able to use.

I have this in my global Module:

Public temps As Templates
Set temps = Application.Templates

Thanks for the great explanation regarding variables and constants!
 
J

JGM

Hi Dragon,

Sorry, I had my head up my proverbial behind!

You cannot use a Set statement in the declartation part of a module.
So, you can have
Public temps As Templates
in the declaration part. This will make "temps" global, at least that is my
understanding.

Inside your module, where you "instantiate" the object "temps", use
Set temps = Application.Templates("path_and_name_of_the_Template")
or
Set temps = Application.Templates(Index_number)
if you know which template you want in the collection.

You cannot have
Set temps = Application.Templates

This does not create a valid object (which template is "temps" supposed to
be?), you get a type mismatch.

HTH
Cheers!
 
P

Perry

Here's a comprehensive explanation on variables
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/ht
ml/scopelifetimeofvariables.asp

(Note: mind the word wrapping; the URL runs from http: through to .asp)

As you will notice from the above article, of course you can carry your
object variable (temps) through the lifecycle of code execution-time/runtime
but

- you can't predict errors that will occur causing the global object
variable to loose it's reference
If the reference is lost and other code/routines require feedback from your
global object variable
you can have a hard time tracking the error down.

- public/global variables take up system resources, if used in abundance

Next to other disadvantages, of course there's one advantage:
having to declare it once, and applicable to all routines within your code.

As a developer, you'll have to outweigh the disadvantages against this
one advantage.

I wouldn't use a one line global declaration, better to instantiate the
object variable when yr userforms gets loaded, use it for it's purpose
but terminate the object variable when yr userforms gets unloaded, thus
reclaiming system resources.

Do the same for the other userforms, should they need the object.

'Global/standard module
Dim temps As Templates

'Userform codemodule go
Private Sub Userform_INitialize()
'here comes the instantiating line:
Set temps = Application.Templates
'other code
End Sub

Private Sub UserForm_Terminate()
'other code
On Error Resume Next
Set temps = Nothing
Err.Clear
End Sub

Krgrds,
Perry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top