?
Solved

Excel 2016 ADO connection and Access 2016

Posted on 2016-09-07
2
Medium Priority
?
634 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
[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
2 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 41788553
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
ID: 41813627
I couldn't repeat the problem. It did not happen again.
However, Scott's advice is good.
0

Featured Post

Independent Software Vendors: 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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

765 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