Solved

VB Connect to ADODB Only if Necesary

Posted on 2007-12-03
3
202 Views
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.
0
Comment
Question by:UPRRDevelopers
3 Comments
 
LVL 20

Accepted Solution

by:
hes earned 250 total points
ID: 20397545
Try this

With NotificationsConn

        If .State = adStateClosed Then .Open

End With

Open in new window

0
 

Author Comment

by:UPRRDevelopers
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.




0
 
LVL 12

Assisted Solution

by:jkaios
jkaios earned 250 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.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now