By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,933 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,933 IT Pros & Developers. It's quick & easy.

testing for association

P: 36
Hi,

How can I test if a variable was associated with an object or not

for example
dim mytextbox as textbox
if mytextbox = Nothing then <=== this produces error
mytextbox = textbox0
else
set mytextbox = Nothing
endif
Jan 7 '07 #1
Share this Question
Share on Google+
6 Replies


ADezii
Expert 5K+
P: 8,638
Hi,

How can I test if a variable was associated with an object or not

for example
dim mytextbox as textbox
if mytextbox = Nothing then <=== this produces error
mytextbox = textbox0
else
set mytextbox = Nothing
endif
You can use the IsEmpty() Function which returns a Boolean value indicating whether or not a Variable has been initialized:

Dim MyTextBox As TextBox
Set MyTextBox = Me![txtTest]
Debug.Print
Jan 7 '07 #2

ADezii
Expert 5K+
P: 8,638
Hi,

How can I test if a variable was associated with an object or not

for example
dim mytextbox as textbox
if mytextbox = Nothing then <=== this produces error
mytextbox = textbox0
else
set mytextbox = Nothing
endif
You can use the IsEmpty() Function which returns a Boolean value indicating whether or not a Variable has been initialized:
Expand|Select|Wrap|Line Numbers
  1. Dim MyTextBox As TextBox
  2. Set MyTextBox = Me![txtTest]     'valid Text Box on frmTest
  3. Debug.Print IsEmpty(MyTextBox) ===> False
  4.  
  5. Debug.Print IsEmpty(MyTextBox2) ==> True
Jan 7 '07 #3

P: 36
Hi,

How can I test if a variable was associated with an object or not

for example
dim mytextbox as textbox
if mytextbox = Nothing then <=== this produces error
mytextbox = textbox0
else
set mytextbox = Nothing
endif
I found out that typename(mytextbox) before association returns Nothing
and after association returns TextBox

The weird thing is this still produces error

if typename(mytextbox) = Nothing then
Jan 7 '07 #4

P: 36
You can use the IsEmpty() Function which returns a Boolean value indicating whether or not a Variable has been initialized:
Expand|Select|Wrap|Line Numbers
  1. Dim MyTextBox As TextBox
  2. Set MyTextBox = Me![txtTest]     'valid Text Box on frmTest
  3. Debug.Print IsEmpty(MyTextBox) ===> False
  4.  
  5. Debug.Print IsEmpty(MyTextBox2) ==> True

But if you set mytextbox2 as textbox isnull would still returns true even if you don't associate it to a valid text box
Jan 7 '07 #5

P: 36
I found out that typename(mytextbox) before association returns Nothing
and after association returns TextBox

The weird thing is this still produces error

if typename(mytextbox) = Nothing then

If found the answer

if Typename(mytextbox) = "Nothing" WORKS
Jan 7 '07 #6

NeoPa
Expert Mod 15k+
P: 31,492
Hi,

How can I test if a variable was associated with an object or not

for example
dim mytextbox as textbox
if mytextbox = Nothing then <=== this produces error
mytextbox = textbox0
else
set mytextbox = Nothing
endif
You need to check 'Is Nothing' and you need to assign the object using 'Set'.
To rewrite your code above :
Expand|Select|Wrap|Line Numbers
  1. Dim mytextbox As TextBox
  2. If mytextbox Is Nothing Then  <=== this shouldn't
  3.     Set mytextbox = textbox0
  4. Else
  5.     Set mytextbox = Nothing
  6. EndIf
Jan 8 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.