2013-03-04 75 views
0

我想从Excel表格中使用VB宏将数据加载到我的SQL Server 2008 R2数据库。 我已经写了工作正常的一个表中的宏,但是当我修改它的数据插入到另一个表我正在错误需要与ADODB VBA宏相关的帮助SQL SERVER

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,日期时间,日期时间的和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 

`

回答

0

我想你会发现,表是ADO的保留字。

+0

不,我在发布学生之前更名表名,实际上我已经重新命名了数据字段,并且专门删除了连接字符串 – bourne 2013-03-04 07:57:50

+0

您还将变量loop1初始化为true,然后用它来确认循环已经在至少一次。 – 2013-03-04 11:45:09

0

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

带有ADO的日期文字格式通常是#mm/dd/yy#,我认为如果您正在使用的特定驱动程序支持它,则只能使用任何其他文字格式。用引号括起来应该解决这个问题。