Scoop question!

  • Thread starter Thread starter Niklas Östergren
  • Start date Start date
N

Niklas Östergren

Hi!

What scoop does a variable have if I declare it like this (Dim strSQL As
String)? Is it avalible through the whole code module or is it just in the
sub/function?

I´d like to declare a variable so I can use it through the whole module but
not as public (i guess).
What I´m trying to do is this:

I have several QBF fom´s. In one of them I let the user select which type of
e-mial address to display (Private, Home, Work etc.). I create a WHERE
clause which I use to create a table containing the persons ID which meet
the critera. Now I also need to use this WHERE clause to create another
table but then holding EmailID instead of PersonID so my thought is to
create a new function but based on another query different from the one with
PersonID.

Well this is not a good explanation so maby it´s easyer for you if you just
know what I´m after!? Which is to use the same variable in two different
functions in the same form module.

TIA!
// Niklas
 
If you declare the variable inside a procedure (Sub or Function) it is only
visible within that procedure. If you declare it outside any procedure
(usually at the top of the module, just after the Option Explicit and Option
Compare statements) it is visible to any procedure within that module if you
declare it using the Private keyword (Private VariableName As DataType) or
to any procedure in any module if you declare it using the Public keyword
(Public VariableName As DataType).

I can't remember off-hand what the default scope is (Public or Private) if
you declare a variable with Dim outside of a procedure. It's documented in
the help files somewhere - try searching for 'scope' and/or 'lifetime'. It
is, in my opinion, better to be explicit and use either Public or Private.
If nothing else, the code will be more readable.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 
Some code that will hopefully explain:

Cheers,

ChrisM


Public publicGlobalString 'This can be seen both anywhere in this module and
also from outside by other modules (in the same project)
Private globalString as string 'This is available anywhere in the module,
but not from outside
Dim anotherGlobalString as string ' The same as the above line

Private Sub SomeSubRoutine(localParameter as String) ' localParameter can
only be seen inside this Sub.
dim localString ' This can only be seen from inside this Subroutine

' So I can do any of these...
localString = publicGlobalString
localString = globalString
localString = anotherGlobalString
localString = localParameter

End Sub

Private Sub AnotherSubFunction(anotherLocalParam)
' I can see publicGlobalString, globalString and anotherGlobalString
from in here
' (and of course anotherLocalParam)
' But not localString
'(or localParameter)
End Sub

'BUT I can't do this
publicGlobalString = localString
' or this:
publicGlobalString = localParameter
'or this
publicGlobalString = anotherLocalParameter
 
Thank´s a lot!

I have read about it, offcourse but I couldn´t find it and didn´t
remembered!
Thanks for helping out!

// Niklas
 
Thanks Chris!

Usefull!

// Niklas


ChrisM said:
Some code that will hopefully explain:

Cheers,

ChrisM


Public publicGlobalString 'This can be seen both anywhere in this module and
also from outside by other modules (in the same project)
Private globalString as string 'This is available anywhere in the module,
but not from outside
Dim anotherGlobalString as string ' The same as the above line

Private Sub SomeSubRoutine(localParameter as String) ' localParameter can
only be seen inside this Sub.
dim localString ' This can only be seen from inside this Subroutine

' So I can do any of these...
localString = publicGlobalString
localString = globalString
localString = anotherGlobalString
localString = localParameter

End Sub

Private Sub AnotherSubFunction(anotherLocalParam)
' I can see publicGlobalString, globalString and anotherGlobalString
from in here
' (and of course anotherLocalParam)
' But not localString
'(or localParameter)
End Sub

'BUT I can't do this
publicGlobalString = localString
' or this:
publicGlobalString = localParameter
'or this
publicGlobalString = anotherLocalParameter





type
 
Back
Top