Results 1 to 4 of 4

System Error in ADODB connection code

Threaded View

  1. #1
    Registered User
    Join Date
    04-21-2009
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2007
    Posts
    42

    System Error in ADODB connection code

    Hi all,

    I'm currently trying to add some functionality into a form I have developed where we can append the results to an existing table in Access 2007. I am testing some code at the moment but getting an error that I can't seem to get round. The Error appears in a 'Microsoft Visual Basic' window and the message states: System Error &H80004005 (-2147467259). The error appears when I attempt to run the following code:

    Sub GetExcelDataToAccess_ADO()
    ' exports data from active worksheet to a table in an Access database
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, i As Long, lastrow As Long
    ' connect to the Access database
    Set cn = New ADODB.Connection
    'define and open a recordset from the specified database path and database table name. Change it to suit your need.
    
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & " Data Source=C:\Documents and Settings\882732\My Documents\QMS Functionality Test 1.accdb;"
    
    Set rs = New ADODB.Recordset
    rs.Open "Test_Table", cn, adOpenKeyset, adLockOptimistic, adCmdTable
    
    lastrow = ActiveSheet.UsedRange.Rows.Count
    For i = 2 To lastrow  ' start from row 2 in the worksheet
    With rs
       .AddNew    ' create a new record
        ' add values to each field in the record
       .Fields("Staff Name") = Range("A" & i).Value
       .Fields("Staff ID") = Range("B" & i).Value
       .Fields("Load Date") = Range("C" & i).Value
       .Fields("QMS Score") = Range("D" & i).Value
       .Update  ' stores the new record in Test_Table
    End With
    
    Next i
    ' close the connection
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
    End Sub
    This is something I have stolen from the internet, I am very new to VBA so basic editing of existing code is about my limit. I have checked Microsoft ActiveX Data Objects 2.8 Library in Tools>References.

    I've searched for a similar problem on the forum but couldn't see one, apologies if I've missed something glaringly obvious.

    Many thanks in advance

    Jon
    Last edited by romperstomper; 11-08-2011 at 12:29 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1