2016-07-29 43 views
0

我試圖通過一個表,獲取相關信息,然後將其插入到VFP9 .dbf表中運行循環。但是,我不斷收到自動化錯誤('-2147217913(80040e07)')。它似乎第一次運行得很好,在錯誤輸出之前一次插入表格中。每次將代碼的執行部分註釋掉時,我都會將它打印出來,但SQL看起來非常好。這裏有什麼問題?循環中使用SQL查詢的自動化錯誤

Dim conn As ADODB.Connection 
Dim rs As ADODB.Recordset 
Dim sConnString As String 

sConnString = "DSN=Visual FoxPro Tables;UID=;SourceDB=s:\accounting\db;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;" 

Set conn = New ADODB.Connection 
Set rs = New ADODB.Recordset 
conn.Open sConnString 

For i = 1 To [RawTable].Rows.Count 
     vStatement = "dong!" 
     vAccount = ActiveSheet.ListObjects("RawTable").DataBodyRange.Cells(i, ActiveSheet.ListObjects("RawTable").ListColumns("account").Index) 
     vCardUser = ActiveSheet.ListObjects("RawTable").DataBodyRange.Cells(i, ActiveSheet.ListObjects("RawTable").ListColumns("card member").Index) 
     vDate = ActiveSheet.ListObjects("RawTable").DataBodyRange.Cells(i, ActiveSheet.ListObjects("RawTable").ListColumns("date").Index) 
     vDesc = ActiveSheet.ListObjects("RawTable").DataBodyRange.Cells(i, ActiveSheet.ListObjects("RawTable").ListColumns("description").Index) 
     vAmount = ActiveSheet.ListObjects("RawTable").DataBodyRange.Cells(i, ActiveSheet.ListObjects("RawTable").ListColumns("amount").Index) 

     MsgBox "INSERT INTO amex_dist (Statement,Account,Card_user,Date,Desc,Amount) VALUES ('" & vStatement & "','" & vAccount & "','" & vCardUser & "','" & vDate & "','" & vDesc & "'," & vAmount & ")" 

     conn.Execute ("INSERT INTO amex_dist (Statement,Account,Card_user,Date,Desc,Amount) VALUES ('" & vStatement & "','" & vAccount & "','" & vCardUser & "','" & vDate & "','" & vDesc & "'," & vAmount & ")") 
Next i 
MsgBox "done :)", vbInformation 
If CBool(conn.State And adStateOpen) Then conn.Close 
Set conn = Nothing 

編輯:這裏是一個什麼樣子表的樣子。

date  receipt description card member account #   amount account 
07/01/2016   Purchase  Employee XXXX-XXXXXX-XXXXX 9.95 41000-000-00 
07/01/2016   Purchase  Employee XXXX-XXXXXX-XXXXX 33  41000-000-00 
06/29/2016   Purchase  Employee XXXX-XXXXXX-XXXXX 64  41000-000-00 
+0

使用'ADO.Command'來傳遞INSERT,而不是直接在連接上使用'.Execute'。 – Comintern

回答

0

Visual Foxpro不喜歡接收日期,只是String文字或一系列數字。嘗試使用CTOD函數(字符到日期),看看是否可以解決問題。 Execute行應如下所示:

conn.Execute ("INSERT INTO amex_dist (Statement,Account,Card_user,Date,Desc,Amount) VALUES ('" & vStatement & "','" & vAccount & "','" & vCardUser & "',CTOD('" & vDate & "'),'" & vDesc & "'," & vAmount & ")")