嘗試從Excel工作表導入數據並追加到Access表中。它讀取第一個值並追加它,但出於某種原因,第二個值不會被解釋爲數據。從Excel中導入的數據未附加到Access中
我認爲這是data4變量的格式問題,或者它在qs行中被調用的方式。任何幫助?
Public Sub Import2(FileName As Variant)
Dim wb As Object, ws As Object
Dim xl As Object
Set xl = CreateObject("excel.Application")
Dim qs As String
Dim oConn As Object
Set oConn = CreateObject("adodb.connection")
oConn.ConnectionString = "Provider=microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Robert\desktop\testMAF.xlsm; Extended Properties=Excel 12.0 XML; hdr=no" & ";"
'Open workbook specified in previous procedure and import data from specified cells
Set wb = xl.Workbooks.Open(FileName)
Set ws = wb.worksheets("For Export")
data1 = ws.cells(2, 1)
Data2 = ws.cells(2, 2)
Data3 = ws.cells(2, 3)
Data4 = ws.cells(2, 4)
'Continues to ...Data62 = ws.cells(2 , 62)
'The following commented code works, entering a date into the Formdate field on the MAF table
'qs = "INSERT INTO MAF (FormDate) VALUES (#" & data1 & "#)"
'However, when I expanded it to include the second field (below)it will not enter the value into the table.
'The locals window shows qs=...Values(#1/4/2010#,Postflight) which is what I want to append
'but when qs is executed, I am prompted, "Enter Parameter Value for Postflight"
qs = "INSERT INTO MAF (FormDate,SN) VALUES (#" & data1 & "#," & Data4 & ")"
DoCmd.RunSQL (qs)
End Sub
不知道你的Access版本,但爲什麼不直接使用[TransferSpreadsheet方法(https://msdn.microsoft.com/en-us/library/office/ff844793.aspx )?否則,您需要將所有「數據」變量標註爲Access中每個字段的確切數據類型。在Excel和Access之間獲取數據類型非常麻煩,必須謹慎對待,否則會發生爆炸。我發現'TransferSpreadsheet'更容易,即使我必須首先格式化excel列(我的意思是通過代碼)。 –