我尝试将特定MS-Excel数据单元(A2,B2,C2)中的数据导入MS-Access表。它适用于以下代码:
CurrentDb.Execute "INSERT INTO [tbl_01] ( Import01, Import02, Import03 ) VALUES('" & strImport01 & "', '" & strImport02 & "', '" & strImport03 & "')"
唯一的问题是,它将数据插入到该访问表上的新数据集中。但我希望实现Excel中的数据应该插入到Access-form“query1”上的选定数据集(ID)中。我不确定为什么它不起作用。你有什么想法吗?
以下是完整的VBA代码:
Set xlapp = CreateObject("Excel.Application")
Dim strImport01 As String
Dim strImport02 As String
Dim strImport03 As String
Dim fileName As String
Set xlwb = xlapp.Workbooks.Open(fileName)
Set xlws = xlwb.Worksheets(1)
fileName = CurrentProject.Path & "\test.xlsx"
strImport01 = xlwb.Sheets(1).Range("A2")
strImport02 = xlwb.Sheets(1).Range("B2")
strImport03 = xlwb.Sheets(1).Range("C2")
CurrentDb.Execute "INSERT INTO [tbl_01] ( Import01, Import02, Import03 ) VALUES('" & strImport01 & "', '" & strImport02 & "', '" & strImport03 & "') SELECT ID WHERE ID =" & [Forms]![query1]![ID]"
答案 0 :(得分:0)
我认为你需要搬家
fileName = CurrentProject.Path & "\test.xlsx"
到顶部。
Dim strImport01 As String
Dim strImport02 As String
Dim strImport03 As String
Dim fileName As String
fileName = CurrentProject.Path & "\test.xlsx"
Set xlapp = CreateObject("Excel.Application")
Set xlwb = xlapp.Workbooks.Open(fileName)
Set xlws = xlwb.Worksheets(1)
希望有所帮助。
答案 1 :(得分:0)
请尝试使用此代码
不需要Select after where子句
" INSERT INTO [tbl_01] " _
& "( Import01, Import02, Import03 ) VALUES(" _
& strImport01 & "', '" & strImport02 & "', '" & strImport03 & ")" _
& "WHERE ID =" & [Forms]![query1]![ID];"
答案 2 :(得分:0)
对于任何有相同或类似问题的人来说......我的解决方案是UPDATE声明:
fileName = CurrentProject.Path & "\test.xlsx"
Set xlapp = CreateObject("Excel.Application")
Dim strImport01 As String
Dim strImport02 As String
Dim strImport03 As String
Set xlwb = xlapp.Workbooks.Open(fileName)
Set xlws = xlwb.Worksheets(1)
strImport01 = xlwb.Sheets(1).Range("A2")
strImport02 = xlwb.Sheets(1).Range("B2")
strImport03 = xlwb.Sheets(1).Range("C2")
CurrentDb.Execute "UPDATE [tbl_01] " & _
" SET Import01 = '" & strImport01 & "' , Import02 = '" & strImport02 & "', Import03 = '" & strImport03 & "' " & _
" WHERE ID =" & [Forms]![query1]![ID]