【问题标题】:Access Excel SQL Insert Into Query访问 Excel SQL 插入查询
【发布时间】:2017-09-25 15:56:18
【问题描述】:

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

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

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

【问题讨论】:

  • 我不确定,但看起来您正在寻找 update 声明。或者,您可能只想在从 Excel 重新导入之前删除所有记录。
  • 是的,我认为你是对的。我并没有对此表示强烈反对。我现在的解决方案: CurrentDb.Execute "UPDATE [tbl_01] SET Import01 = '" & strImport01 & "' , Import02 = '" & strImport02 & "', Import03 = '" & strImport03 & "' WHERE ID =" & [Forms] ![query1]![ID]
  • 谢谢 GolezTrol .. 你为我节省了一些时间 :)

标签: sql excel vba ms-access


【解决方案1】:

我觉得你需要搬家

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)

希望对您有所帮助。

【讨论】:

    【解决方案2】:

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

    where 子句后不需要 Select

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        • 1970-01-01
        • 2013-11-06
        相关资源
        最近更新 更多