Cannot access an ACCDB table from Excel

USAMax

Well-known Member
Joined
May 31, 2006
Messages
843
Office Version
  1. 365
Platform
  1. Windows
I have this code that looks like it will do everything I need but it will not make the connection. adStateOpen is always empty.


Code:
Function MakeConnection(ImptDB As String, TableName As String) As Boolean
'*********Routine to establish connection with database

Dim DBFullName As String
Dim cs As String

DBFullName = Application.ActiveWorkbook.Path & "\ImptDB"

cs = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"

Set cn = CreateObject("ADODB.Connection")

If Not (cn.State = adStateOpen) Then
cn.Open cs
End If

Set rs = CreateObject("ADODB.Recordset")

If Not (rs.State = adStateOpen) Then

   rs.Open TableName, cn, adOpenKeyset, adLockOptimistic

End If

End Function

Sorry but I am new to working with Access from Excel.
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I'm sorry but I made a very clear mistake and I corrected it but it still does not work:

Code:
Public Function MakeConnection(ImptDB As String, TableName As String) As Boolean
'*********Routine to establish connection with database
'    Workbooks.OpenDatabase Filename:="C:\Users\Dave\Documents\Tibra\DB.accdb", _
        CommandText:=Array("tblInstrumentsInfo"), CommandType:=xlCmdTable, _
        ImportDataAs:=xlTable

Dim DBFullName As String
Dim cs As String

DBFullName = Application.ActiveWorkbook.Path & "\" & ImptDB

cs = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"

Set cn = CreateObject("ADODB.Connection")

If Not (cn.State = adStateOpen) Then
cn.Open cs
End If

Set rs = CreateObject("ADODB.Recordset")

If Not (rs.State = adStateOpen) Then

   rs.Open TableName, cn, adOpenKeyset, adLockOptimistic

End If

End Function

Thanks,
 
Upvote 0
I found I was missing a refference, "Microsoft ActiveX Data Objects Recordset 6.0 Library" I add and the adStateOpen is now set to 1!


I am making progress but, now I have a run-tim error '-2147467259 (80004005)'

Unrecognized database format
'C:Users\David\Documents\DB.accdb'


I think it has something to do with the line, "cs = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"

Any ideas.... anybody?
 
Upvote 0
I found the problem was with the old OLEDB:

Code:
Public Function MakeConnection(ImptDB As String, TableName As String) As Boolean
'*********Routine to establish connection with database

'Dim db As database, rs As Recordset, r As Long
Dim DBFullName As String
Dim cs As String

DBFullName = Application.ActiveWorkbook.Path & "\" & ImptDB

cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DBFullName & ";"

Set cn = CreateObject("ADODB.Connection")
'Set cn = OpenDatabase(DBFullName)

If Not (cn.State = adStateOpen) Then
cn.Open cs
End If

Set rs = CreateObject("ADODB.Recordset")

If Not (rs.State = adStateOpen) Then

   rs.Open TableName, cn, adOpenKeyset, adLockOptimistic

End If

End Function

Now that I got the connection working I have to find out why rcount is not working but that will have to be another thread.
 
Upvote 0

Forum statistics

Threads
1,216,049
Messages
6,128,496
Members
449,455
Latest member
jesski

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top