Excel シートから VB マクロを使用して、SQL Server 2008 R2 データベースにデータをロードしようとしています。あるテーブルで正常に動作するマクロを既に作成しましたが、それを変更して別のテーブルにデータを挿入すると、エラーが発生します
Runtime Error'2147217900(80040e14)'
[microsoft][ODBC SQL Server Driver][SQL SERVER]Incorrect syntax near '9'
デバッグを押すと、この行が強調表示されます
oCm.Execute iRecAffected
更新しようとしているテーブルには、int、int、int、datetime、datetime、および int 型の a、b、c、d、e、f の 6 つのフィールドがあります。
この行の日時フィールドに対して行っていないことがあるために、エラーが発生したと思います
oCm.CommandText = "Insert Into Table (a, b, c, d, e, f) Values (" & a & " ," & b & " ," & c & "," & d & "," & e & "," & f & ")"
何が問題なのか教えてください。
`
Dim DestinationWorkBook As Workbook
Dim SourceWorkBook As Workbook
Dim DestinationWorkBook1 As Workbook
Dim SourceWorkBook1 As Workbook
Dim oCm As ADODB.Command
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Date
Dim e As Date
Dim f As Integer
Dim iRecAffected As Integer
Dim CurrentRow As Integer
Set DestinationWorkBook = ThisWorkbook
Set SourceWorkBook = ThisWorkbook
Set DestinationWorkBook1 = ThisWorkbook
Set SourceWorkBook1 = ThisWorkbook
Dim b As String
Dim rngRange As Range
Dim a As String
LoopContinue = True
CurrentRow3 = 2
loop1 = True
CurrentRow = 2
DestinationWorkBook.Activate
Worksheets("errors").Select
Rows("1:1500").Select
Selection.Delete Shift:=xlUp
SourceWorkBook.Activate
Worksheets("Final_output").Select
Dim oCon As ADODB.Connection
Dim oRs As ADODB.Recordset
Set oCon = New ADODB.Connection
oCon.ConnectionString = "deleted"
oCon.Open
Set oCm = New ADODB.Command
oCm.ActiveConnection = oCon
While loop1 = True
a = (Range("A" & CStr(CurrentRow)).Value)
b = (Range("B" & CStr(CurrentRow)).Value)
c = (Range("C" & CStr(CurrentRow)).Value)
d = (Range("D" & CStr(CurrentRow)).Value)
e = (Range("E" & CStr(CurrentRow)).Value)
f = (Range("F" & CStr(CurrentRow)).Value)
If Len(Range("A" & CStr(CurrentRow)).Value) = 0 Then
loop1 = False
End If
If (loop1 <> False) Then
oCm.CommandText = "Insert Into Table (a, b, c, d, e, f) Values (" & a & " ," & b & " ," & c & "," & d & "," & e & "," & f & ")"
oCm.Execute iRecAffected
CurrentRow = CurrentRow + 1
End If
Wend
oCon.Close
End Sub
`