Solved

Excel 2016 ADO connection and Access 2016

Posted on 2016-09-07
2
89 Views
Last Modified: 2016-09-24
When I run the following code a connection is made to an Access database. Ok.
However, if the database is open I get an error.

Is it possible to connect to an Access database with ADO like this AND have the database open?
If so how?

Thanks

Public objConn As New ADODB.Connection

Sub DBConnectionAccess()

    If CBool(objConn.State And adStateOpen) Then objConn.Close

    Dim strPathToDB As String
                 
    'open connection to Access database
    strPathToDB = "C:\MyTest\Database\test.accdb"
    objConn.Open "Provider = Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & strPathToDB

    Exit Sub

End Sub

Open in new window

0
Comment
Question by:hindersaliva
2 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
Comment Utility
Anytime you get an error you should post full details of that error (and a screenshot of the error message is good too). Otherwise we're just guessing.

Do you mean if the CONNECTION is open you get an error? That would seem to be the case, since you can definitely connect to an "open" Access database (i.e. one open in the Access window).

Also:

If the connection is open, why close it just to reopen it? It would seem a more logical flow would be:

If Not CBool(objConn.State And adStateOpen) Then
   Set objConn = New ADODB.Connection
   objConn.Open "Provider = Microsoft.ACE.OLEDB.12.0;" & "Data Source="C:\MyTest\Database\test.accdb"
End If
0
 

Author Closing Comment

by:hindersaliva
Comment Utility
I couldn't repeat the problem. It did not happen again.
However, Scott's advice is good.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

11 Experts available now in Live!

Get 1:1 Help Now