ADO - Catastrophic error

mrllewellyn

New Member
Joined
Apr 7, 2010
Messages
10
This is the first time I've tried to set up ADO in an Excel file to get from an Access file. I copied this code from here with minor changes for the difference in the file name and worksheets and I get a catastrophic error. I'm using Excel and Access 2007. Any suggestions?:confused:

HTML:
'http://p2p.wrox.com/excel-vba/76031-how-specify-relative-path-access-2007-ado-connection-string-excel-2007-a.html
'Recordsets http://allenbrowne.com/ser-29.html
'http://www.codeproject.com/KB/database/connectionstrings.asp

Sub ADO_Demo()
    Dim DBFullName As String
    Dim Cnct As String, Src As String
    Dim Connection As ADODB.Connection
    Dim Recordset As ADODB.Recordset

'''''''''''''''''''''''''''''''''''''''''''''''''''Database information
DBFullName = ThisWorkbook.Path & "\Database1.accdb"

'''''''''''''''''''''''''''''''''''''''''''''''''''Open the connection
Set Connection = New ADODB.Connection
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct






'''''''''''''''''''''''''''''''''''''''''''''''''''Create the recordset
 Set Recordset = New ADODB.Recordset
    With Recordset
        Src = "SELECT * FROM Test"
        .Open Source:=Src, ActiveConnection:=Connection
    
        'Write the field names
        For Col = 0 To Recordset.Fields.Count - 1
            Worksheets("Sheet1").Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
        Next
    
        'Write the recordset
        Worksheets("Sheet1").Range("A1").Offset(1, 0).CopyFromRecordset Recordset
    End With
    
    Set Recordset = Nothing
    Connection.Close
    Set Connection = Nothing


End Sub
 
Last edited by a moderator:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
First thing to think of have you set the references to ADODB in the VBA Screen?

Next have you adjusted the Database name and location to your own. Have you renamed the code to look for your table in the database?

Finally where does it stop working and what does the debug state?
 
Upvote 0
These are the references I'm using.

untitled-2.jpg
 
Upvote 0
The code has changed a little. I am using an example of a coworker now and I created a different test file. He uses .mdb so I did too. I don't know of a reference to check what is the correct .Provider or reference. My error says:"Run-time error '-2147418113"


HTML:
Sub ADO_Demo()
    Dim DBFullName As String
    Dim Cnct As String, Src As String
    Dim Connection As ADODB.Connection
    Dim Recordset As ADODB.Recordset

'''''''''''''''''''''''''''''''''''''''''''''''''''Database information
DBFullName = "P:\NC Finance\Katie\Katie\test.mdb"

'''''''''''''''''''''''''''''''''''''''''''''''''''Open the connection
Set Connection = New ADODB.Connection
With Connection
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Open DBFullName  ' ****this is where it goes to in debug mode
End With
 
Upvote 0
I have just copied the code you have used, set the reference you have and all works fine. I have adjusted the path to a database and database name and also the table name, it returns the data, so where does it stop working and what is the message?

Sub ADO_Demo()
Dim DBFullName As String
Dim Cnct As String, Src As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
'''''''''''''''''''''''''''''''''''''''''''''''''''Database information
DBFullName = "M:\Access Files\Acc 2000 Sample Queries" & "\QrySmp00.mdb"
'''''''''''''''''''''''''''''''''''''''''''''''''''Open the connection
Set Connection = New ADODB.Connection
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct
'''''''''''''''''''''''''''''''''''''''''''''''''''Create the recordset
Set Recordset = New ADODB.Recordset
With Recordset
Src = "SELECT * FROM Employees"
.Open Source:=Src, ActiveConnection:=Connection

'Write the field names
For Col = 0 To Recordset.Fields.Count - 1
Worksheets("Sheet1").Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
Next

'Write the recordset
Worksheets("Sheet1").Range("A1").Offset(1, 0).CopyFromRecordset Recordset
End With

Set Recordset = Nothing
Connection.Close
Set Connection = Nothing

End Sub
 
Last edited:
Upvote 0
Thanks for testing the code. So my references are okay?...

I moved both the Access and Excel files to my Desktop for testing. Do I need to turn something on in Access for this to work?

The error still says "Catastrophic Failure" and goes to the red text below.


Sub ADO_Demo1()
Dim DBFullName As String
Dim Cnct As String, Src As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
'''''''''''''''''''''''''''''''''''''''''''''''''''Database information
DBFullName = "C:\Documents and Settings\JLlewelyn\Desktop" & "\test.mdb"
'''''''''''''''''''''''''''''''''''''''''''''''''''Open the connection
Set Connection = New ADODB.Connection
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct



'''''''''''''''''''''''''''''''''''''''''''''''''''Create the recordset
Set Recordset = New ADODB.Recordset
With Recordset
Src = "SELECT * FROM Test"
.Open Source:=Src, ActiveConnection:=Connection

'Write the field names
For Col = 0 To Recordset.Fields.Count - 1
Worksheets("Sheet1").Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
Next

'Write the recordset
Worksheets("Sheet1").Range("A1").Offset(1, 0).CopyFromRecordset Recordset
End With

Set Recordset = Nothing
Connection.Close
Set Connection = Nothing

End Sub
 
Upvote 0
Is the only message 'Catastrophic error'?

Try this before you open the connection.
Code:
Connecton.CursorLocation=adUseClient
 
Upvote 0
When I insert the code:
Connecton.CursorLocation = adUseClient
it says Run-time error 424, Object Required

Before I inserted the code the message was:
Run-time error '-2147418113", Catastrophic Error
 
Upvote 0
Yes your references are OK and no you don't need to do anything to the database. I can't see anything wrong with the code.

A couple of sites suggest memory, security in the applications and problem with installation.
 
Upvote 0
Where did you put the code?

By the way, and I should have spotted this earlier and it may very well be the root of the problem - don't call your Connection object variable Connection.:)
Code:
Sub ADO_Demo1()
Dim DBFullName As String
Dim Cnct As String, Src As String
Dim cnn As ADODB.Connection
Dim Recordset As ADODB.Recordset
 
    '''''''''''''''''''''''''''''''''''''''''''''''''''Database information
    DBFullName = "C:\Documents and Settings\JLlewelyn\Desktop" & "\test.mdb"
    '''''''''''''''''''''''''''''''''''''''''''''''''''Open the connection
    Set cnn = New ADODB.Connection
 
    Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
 
    Cnct = Cnct & "Data Source=" & DBFullName & ";"
 
    cnn.CursorLocation = adUseClient
 
    cnn.Open ConnectionString:=Cnct
 
    '''''''''''''''''''''''''''''''''''''''''''''''''''Create the recordset
    Set Recordset = New ADODB.Recordset
    
    With Recordset
        Src = "SELECT * FROM Test"
        .Open Source:=Src, ActiveConnection:=Connection
        'Write the field names
        For Col = 0 To Recordset.Fields.Count - 1
            Worksheets("Sheet1").Range("A1").Offset(0, Col).Value = Recordset.Fields(Col).Name
        Next
        'Write the recordset
        Worksheets("Sheet1").Range("A1").Offset(1, 0).CopyFromRecordset Recordset
    End With
 
    Set Recordset = Nothing
    
    cnn.Close
    
    Set cnn = Nothing
 
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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