How to pass a boolean

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 2 subs
AddPatient_Click()
Search_Click()

I have a boolean, searchbool, in the Search_Click that is set to true at
some point. What I want to do is use this in AddPatient_Click so how do I
get this boolean to the AddPatient to check and see if it is true?
 
pokdbz said:
I have 2 subs
AddPatient_Click()
Search_Click()

I have a boolean, searchbool, in the Search_Click that is set to true
at some point. What I want to do is use this in AddPatient_Click so
how do I get this boolean to the AddPatient to check and see if it is
true?

I take it these are event procedures, so you can't just redefine the
AddPatient_Click() routine to accept a parameter? If that's the case,
move the Dim statement for searchbool out of the Search_Click()
procedure and up to the General section, Declarations subsection of the
form's code module. That will give the variable module-level scope, so
both procedures will have access to it. Do not add Dim for this
variable to the AddPatient_Click() procedure.

Be aware that moving the variable to the module level will also give the
variable a module-level lifetime, so you can no longer assume, in
Search_Click(), that it has a value of False the first time you refer to
it. You must be sure to initialize it to whatever value you want when
you enter that procedure.
 
If you want to share a variable between procedures in the same module,
declare it using the Private keyword in the Declarations section at the top
of the module (after the Option Compare Database and Option Explicit
statements, and before any procedure definition) ...

Option Compare Database
Option Explict

Private mboolWhatever As Boolean

Public Sub FirstSub()

mboolWhatever = True

End Sub

Public Sub SecondSub()

MsgBox mboolWhatever

End Sub

Don't forget to remove the declaration of the variable from the procedure
where it is now, otherwise they will be two separate and independent
variables. The local variable will be used within the procedure where it is
declared, while the module-level variable will be used by all other
procedures in the module. See the help topics "Understanding the Lifetime of
Variables" and "Understanding Scope and Visibility". (At least, those are
the topic titles in Access 2003. They may vary somewhat in earlier
versions.)


--
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.
 
Back
Top