Insert in DB mittels VBA
30.12.2008 19:01:00
Rudolf
ich versuche gerade Daten von einem VBA-Excel Modul in eine Access-DB zu schreiben. Leider gibt es immer noch einen Fehler auf den ich nicht draufkomme.
Meine Methode sieht so aus:
Private Sub CommandButton2_Click()
Dim DBFullName As String
Dim Cnct As String, Src As String
Dim Connection As ADODB.Connection
Dim Recordset As ADODB.Recordset
Dim Col As Integer
Cells.Clear
' Database information
DBFullName = ThisWorkbook.Path & "\PMO.mdb"
' Open the connection
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.Jet.OLEDB.4.0; "
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct
' Create RecordSet
Set Recordset = New ADODB.Recordset
Recordset.Open Source:="Tasks", ActiveConnection:=Connection
With Recordset
.AddNew
.Fields("ParentTask").Value = "C00T35"
.Fields("TaskID").Value = "RA-34-2345"
.Fields("Task").Value = "Maintenance"
.Fields("Org").Value = "Maintenance Team"
.Fields("TeamMember").Value = "John Hutch"
.Fields("StartDate").Value = "21.02.2009"
.Fields("DueDate").Value = "01.04.2009"
'add data for the other fields here
.Update
.Close
End With
End Sub
Wenn ich die Prozedur ausführe kommt folgender Fehler:
Run-time error '2147217900 (80040e14)
Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
Im Debug Modus bleibt er bei folgender Codezeile stehen:
Recordset.Open Source:="Tasks", ActiveConnection:=Connection
Ich probiere jetzt schon solange herum und komme auf den Fehler nicht drauf bzw. wie der Code richtig lauten müsste. Hat von euch jemand eine Idee..?
grüsse, rudi