VB Connect to ADODB Only if Necesary

Posted on 2007-12-03
Medium Priority
Last Modified: 2010-04-30
I have a connect string that I am attempting to use in VB to connect only if I do not currently have a connection active.  This is the code:

    If NotificationsConn.State = 0 Then
        SQLString = "Data Source=DSource;UID=UserID;PWD=password"
        NotificationsCmd.ActiveConnection = SQLString
    End If

The problem with this code is that it creates a connection to the database every time it is run, even if there is already a connection present.  What am I doing wrong?  Thanks very much.
Question by:UPRRDevelopers
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
LVL 20

Accepted Solution

hes earned 1000 total points
ID: 20397545
Try this

With NotificationsConn
        If .State = adStateClosed Then .Open
End With

Open in new window


Author Comment

ID: 20397731
Thanks, but I must be doing something wrong, because even if I put your statement directly after mine, when my connect runs and successfully connects, your code still believes "adStateClosed" is true and tries to open the connection.  So this:

    If NotificationsConn.State = 0 Then
        SQLString = "Data Source=DSource;UID=UserID;PWD=password"
        NotificationsCmd.ActiveConnection = SQLString
    End If

With NotificationsConn
        If .State = adStateClosed Then .Open
End With

Always tries to re-open the connection.

LVL 12

Assisted Solution

jkaios earned 1000 total points
ID: 20398617
How does NotificationsConn variable defined?  Is it defined as a "public" variable in a module?

There are some thing to consider here:

1) If the NotificationsConn variable is defined as a "private" variable in a sub-routine, then this variable will be implicitly set to nothing, when the procedure in which it is defined, ends (thus setting the State to adStateClosed).

2) If it is defined as a "public" variable, then that little piece of code will work.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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
Course of the Month15 days, 4 hours left to enroll

771 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