PDA

View Full Version : Test if variable or object exists



zagrijs
02-20-2013, 03:01 AM
Hi

Is there a way to test if a variable or an object exists?

:doh:

gerken
02-20-2013, 03:54 AM
Sub test()
Dim obc As Object
On Error Resume Next
a = obc
If Err.Number <> 0 Then
'code in here
End If
On Error GoTo 0

End Sub

zagrijs
02-20-2013, 05:41 AM
thanks gerken.

However, it doesn't solve my challenge. I'm using different forms that share some procedure. The forms do not all have exactly the same objects and variables.

I am using an error catch at present, but was hoping there is a single command that one can use to test if an object actually exists. (Visual dBase does have such a command)

Kenneth Hobs
02-20-2013, 06:53 AM
For variables, check the value. You should always use Option Explicit as the first line of code in a Module or Userform and avoid variables existing or not.

For objects, use Is Nothing or Is Not Nothing. e.g.
http://www.vbaexpress.com/forum/showthread.php?t=38802

snb
02-20-2013, 07:29 AM
I'm using different forms that share some procedure

In that case you can use a classmodule.