0

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

`

4

2 に答える 2

0

CommandText の datetime 値を一重引用符で囲んでみてください。",'" & d & "','" & e & "',"

ADO の日付リテラル形式は通常#mm/dd/yy#、使用している特定のドライバーでサポートされている場合にのみ、他のリテラル形式を使用できると思います。値を引用符で囲むと、これを回避できます。

于 2013-03-04T22:10:32.480 に答える
0

Table は ADO の予約語であることがわかると思います。

于 2013-03-04T07:55:34.303 に答える