?
Solved

How to find whether object exists?

Posted on 2001-06-17
6
Medium Priority
?
185 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
[X]
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
6 Comments
 
LVL 6

Accepted Solution

by:
sharmon earned 30 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
0
 

Expert Comment

by:ostrosoft
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
...
0
 
LVL 3

Expert Comment

by:RodStephens
ID: 6199663
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:sharmon
ID: 6199693
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
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
      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
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…
Suggested Courses

752 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