Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB Connect to ADODB Only if Necesary

Posted on 2007-12-03
3
Medium Priority
?
254 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
[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
3 Comments
 
LVL 20

Accepted Solution

by:
hes earned 1000 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 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.
0

Featured Post

Technology Partners: 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

636 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