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

我要更新的表有 6 个字段 a、b、c、d、e、f 类型为 int、int、int、datetime、datetime 和 int

我相信错误是因为我没有为这一行中的日期时间字段做一些事情

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

我想你会发现 Table 是 ADO 中的保留字。

于 2013-03-04T07:55:34.303 回答
0

尝试在 CommandText 中的日期时间值周围加上单引号 - 例如",'" & d & "','" & e & "',"

ADO 的日期文字格式通常是#mm/dd/yy#,我认为只有在您使用的特定驱动程序支持的情况下,您才能使用任何其他文字格式。将值括在引号中应该可以解决这个问题。

于 2013-03-04T22:10:32.480 回答