I am on my first attempt to use vba to open an Access database from an Excel file and write the data in the Excel file to the database. Not surprisingly I've run into difficulty.
This is my code which I've tried, but the process of opening the database does not work (in red).
Can someone help me with that?
The goal ist to export data from Excel to a data base in access to store it there.
The error that I have encountered is "Run-time error '3704'
Operation is not allowedwhen the object is closed"
This is my code which I've tried, but the process of opening the database does not work (in red).
Can someone help me with that?
The goal ist to export data from Excel to a data base in access to store it there.
VBA Code:
Sub TEST_DB_import()
Dim ADOC As ADODB.Connection
Dim DBS As ADODB.Recordset
Dim lngZeile As Long
Dim intIndex As Integer
Dim arNamen As Variant
On Error GoTo Fehler
Set ADOC = New ADODB.Connection
With ADOC
.Provider = "Microsoft.Jet.OLEDB.4.0"
[COLOR=rgb(184, 49, 47)].Open "C:\Users\tapa\Desktop\Flagging Mastersplit.accdb"[/COLOR]
End With
Set DBS = New ADODB.Recordset
DBS.Open "Flagging", ADOC, adOpenKeyset, adLockOptimistic
With Sheets("Sheet1")
arNamen = .Range(.Range("A2"), .Range("A2").End(xlToRight))
For lngZeile = 3 To .Range("A3").End(xlDown).Row
DBS.AddNew
For intIndex = 1 To UBound(arNamen, 2)
DBS.Fields(arNamen(1, intIndex)) = .Cells(lngZeile, intIndex).Value
Next
DBS.Update
Next
End With
Fehler:
If Err.Number Then MsgBox Err.Description, , Err.Number
If Not DBS Is Nothing Then DBS.Close
If Not ADOC Is Nothing Then ADOC.Close
Set ADOC = Nothing
Set DBS = Nothing
End Sub
The error that I have encountered is "Run-time error '3704'
Operation is not allowedwhen the object is closed"
Last edited by a moderator: