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:
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.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
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 11:29 AM.
Hi all,
Solved it, not sure exactly what's done it, however, having re-edited the code a few times, changing the access extension to .mdb and also checking:
Microsoft DAO 3.6 Object Library
Microsoft ActiveX Data Objects 2.0 Library
in Tools>References, this seems to have done the job. Now all I need to do is figure out how to add a reference to a database password in there, but that's a potential future issue for another time.
Thanks to everyone who had a look to see if they could help.
Jon
FYI, The Jet provider doesn't understand accdb as it's a newer format. You'd need to use the Microsoft.ACE.OLEDB.12.0 provider instead.
Good to know - thanks!
Jon
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks