【问题标题】:Access VBA: Discard "can't append" message (Primary Key Violation)访问 VBA:丢弃“无法附加”消息(主键违规)
【发布时间】:2025-11-29 13:55:02
【问题描述】:

我正在尝试在 Access 2010 中创建一个宏,它打开一个 excel 文件,在 excel 中运行宏,然后导入给定的结果。我在这个过程中有两个问题。

  1. Application.DisplayAlerts = False 在 Excel 中 然而,DisplayAlerts 不断弹出。我需要在宏 Access 中做一些特别的事情吗?
  2. 警告“由于主键违规而无法追加”不断弹出。我知道问题是什么,但我想忽略它。我可以使用On Error Resume?但我希望最后有一个消息框,其中包含未附加到的表。这可能吗,你能指出我正确的方向吗?我已经尝试了一些错误处理,但我不知道如何在不中断进程的情况下在最后弹出消息。

代码:

Private Sub Main_btn_Click()

        Dim fileImport(0 To 3, 0 To 2) As String

        fileImport(0, 0) = "Stock_CC"
        fileImport(0, 1) = "F:\370\Hyperviseur\SITUATIE\Macro\Stock_getdata.xlsm"
        fileImport(0, 2) = "GetStock"

        fileImport(1, 0) = "Wips_CC"
        fileImport(1, 1) = "F:\370\Hyperviseur\SITUATIE\Macro\Wips_getdata.xlsm"
        fileImport(1, 2) = "Update"

        fileImport(2, 0) = "CCA_cc"
        fileImport(2, 1) = "F:\370\Hyperviseur\SITUATIE\Macro\SLAcc.xls"
        fileImport(2, 2) = "Read_CCA"

        fileImport(3, 0) = "Eps_cc"
        fileImport(3, 1) = "F:\370\Hyperviseur\SITUATIE\Macro\eps.xlsm"
        fileImport(3, 2) = "Update"


        Dim i As Integer
        For i = 0 To UBound(fileImport, 1)
            RunMacroInxcel fileImport(i, 1), fileImport(i, 2)
            transferSpreadsheetFunction fileImport(i, 0), fileImport(i, 1)
        Next i
    End Sub

    Private Sub RunMacroInExcel(fName As String, macroName As String)
      Dim Xl As Object

    'Step 1:  Start Excel, then open the target workbook.
       Set Xl = CreateObject("Excel.Application")
        Xl.Workbooks.Open (fName)
        Xl.Visible = True
          Xl.Run (macroName)
           Xl.ActiveWorkbook.Close (True)
            Xl.Quit
          Set Xl = Nothing


    End Sub

    Private Sub transferSpreadsheetFunction(ByVal tableName As String, ByVal fileName As String)
        If FileExist(fileName) Then
            DoCmd.TransferSpreadsheet acImport, , tableName, fileName, True
        Else
        Dim Msg As String
            Msg = "Bestand niet gevonden" & Str(Err.Number) & Err.Source & Err.Description
            MsgBox (Msg)
          End If
    End Sub


    Function FileExist(sTestFile As String) As Boolean
       Dim lSize As Long
       On Error Resume Next
          lSize = -1
          lSize = FileLen(sTestFile)
       If lSize > -1 Then
          FileExist = True
       Else
          FileExist = False
       End If
    End Function

【问题讨论】:

  • 没有代码很难帮你...
  • 这是给您错误的代码,还是来自 4 个工作簿中正在执行的宏的错误?你打电话给RunMacroInxcel,但有Private Sub RunMacroInExcel。我认为这只是一个复制/粘贴错误。对于您的错误 #1 - 您在哪里设置 Application.DisplayAlerts = False?我在这段代码中没有看到它。

标签: vba excel ms-access-2010


【解决方案1】:

在 For 循环中添加错误处理,连接到字符串变量,然后在消息框中输入字符串:

Dim i As integer, failedFiles as string

failedFiles = "List of failed tables: " & vbNewLine & vbNewLine

For i = 0 To UBound(fileImport, 1) 
   On Error Goto NextFile
      Call RunMacroInxcel(fileImport(i, 1), fileImport(i, 2))
      Call transferSpreadsheetFunction(fileImport(i, 0), fileImport(i, 1)) 

NextFile:
      failedFiles = failedFiles & " " & fileImport(i,0) & vbNewLine 
      Resume NextFile2
NextFile2:
Next i

MsgBox failedFiles, vbInformation, "Failed Tables List"

【讨论】: