Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

How to find whether object exists?

Posted on 2001-06-17
Last Modified: 2012-05-04
Consider a situation that I have an ADO connection objet named cn. Now I have closed the object as :


and have released the resources as :

Set cn = Nothing.

Now if I call some routine that uses the ADO cn object and refer to it program would give me an error. I want that my program must first confirm whether this object exists then refer to it. HGow can I do this.
Question by:ShehzadMunir

Accepted Solution

sharmon earned 10 total points
ID: 6199634
Dim cn As ADODB.Connection

Private Sub Form_Load()

  Set cn = New ADODB.Connection
  'Setup and open, etc...

  If cn.State = adStateOpen Then cn.Close
  Set cn = Nothing

  Dim fResult As Boolean
  fResult = cn Is Nothing
  Debug.Print fResult
End Sub

Expert Comment

ID: 6199659
On Error Resume Next
If cn.State = adStateClosed Then MsgBox "Connection closed"
If Err <> 0 Then MsgBox "Object destroyed"
On Error GoTo 0

Expert Comment

ID: 6199663
If cn Is Nothing Then
    ' Connection doesn't exist...
    ' Connection exists...
End If

To see if the connection is open, use:

    If cn.State = adStateOpen Then ...
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Expert Comment

ID: 6199693
ostrosoft, closing a connection doesn't destroy the object.

RodStephens, your code is basically the same as mine...

Expert Comment

ID: 6200155
Checking the cn.state after Set cn = Nothing will cause an error Try this code

Function ChkValidConnection()
On Error GoTo ErrorHandler:

'Just to Check
Set conn = Nothing

Chkconn = IsObject(conn)
If Chkconn = True Then
    If conn.State = 1 Then
    End If
    'use this if necessary
    Dim conn As ADODB.Connection
    Set conn = New ADODB.Connection
End If

conn.Open "ConnectionString"

  Exit Function

  If Err = 91 Then  ''Object variable not set
      Set conn = New ADODB.Connection
      MsgBox "Unhandled Error"
      Resume ErrorHandlerExit:
  End If

End Function

Author Comment

ID: 6200946
Because object cannot be referred after being destroyed.

Actually right now I am not seeing any Accept Answer button. Though I have accepted sharmon 's answer in a sense that we can check the connection existance by keeping a boolean variable. : Hornet241 is absolutely right. But since Sharmon attempted first so no he would be awarded the marks.

Thanks for all who answered.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
passing a value with stream reader AFTER a ";" 3 74
Spell Check in VB6 13 126
VB6 ListBox Question 4 48
pop out of webbrowser1 control vba6 5 24
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question