繁体   English   中英

访问Excel SQL插入查询

[英]Access Excel SQL Insert Into Query

我尝试将数据从特定的MS-Excel数据单元(A2,B2,C2)导入到MS-Access表中。 它适用于以下代码:

CurrentDb.Execute "INSERT INTO [tbl_01] ( Import01, Import02, Import03 ) VALUES('" & strImport01 & "', '" & strImport02 & "', '" & strImport03 & "')"

唯一的问题是,它将数据插入该访问表上的新数据集中。 但是我想实现将数据克隆Excel插入到Access表单“ 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]"

我想你需要搬家

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)

希望对您有所帮助。

试试这个代码,而不是你的

在where子句中不需要Select

" INSERT INTO [tbl_01] " _ 
        & "( Import01, Import02, Import03 ) VALUES(" _ 
    & strImport01 & "', '" & strImport02 & "', '" & strImport03 & ")" _
        & "WHERE ID =" & [Forms]![query1]![ID];" 

对于任何有相同或相似问题的人..我对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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM