[英]Need Help related to ADODB VBA Macro SQL SERVER
我正在嘗試使用 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
`
我想你會發現 Table 是 ADO 中的保留字。
嘗試在 CommandText 中的日期時間值周圍加上單引號 - 例如",'" & d & "','" & e & "',"
ADO 的日期文字格式通常是#mm/dd/yy#
,我認為您只能使用任何其他文字格式,前提是您正在使用的特定驅動程序支持它。 將值括在引號中應該可以解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.