Consulting

Results 1 to 5 of 5

Thread: Test if variable or object exists

  1. #1
    VBAX Regular
    Joined
    Sep 2011
    Posts
    78
    Location

    Test if variable or object exists

    Hi

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


  2. #2
    VBAX Newbie
    Joined
    Feb 2013
    Posts
    2
    Location

    Catch error

    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

  3. #3
    VBAX Regular
    Joined
    Sep 2011
    Posts
    78
    Location
    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)

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    I'm using different forms that share some procedure
    In that case you can use a classmodule.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •