Solved

How to find whether object exists?

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

cn.close

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.
0
Comment
Question by:ShehzadMunir
6 Comments
 
LVL 6

Accepted Solution

by:
sharmon earned 10 total points
Comment Utility
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
0
 

Expert Comment

by:ostrosoft
Comment Utility
...
On Error Resume Next
If cn.State = adStateClosed Then MsgBox "Connection closed"
If Err <> 0 Then MsgBox "Object destroyed"
On Error GoTo 0
...
0
 
LVL 3

Expert Comment

by:RodStephens
Comment Utility
If cn Is Nothing Then
    ' Connection doesn't exist...
Else
    ' Connection exists...
End If

To see if the connection is open, use:

    If cn.State = adStateOpen Then ...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:sharmon
Comment Utility
ostrosoft, closing a connection doesn't destroy the object.

RodStephens, your code is basically the same as mine...
0
 
LVL 3

Expert Comment

by:Hornet241
Comment Utility
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
      conn.Close
    End If
  Else
    'use this if necessary
    Dim conn As ADODB.Connection
    Set conn = New ADODB.Connection
End If

conn.Open "ConnectionString"

ErrorHandlerExit:
  Exit Function

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

End Function
0
 

Author Comment

by:ShehzadMunir
Comment Utility
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now