1

我有一些 VBA 代码询问用户是否希望在他们关闭 MS Access 中的表单之前保存记录。这是代码的样子:

Private Sub Form_BeforeUpdate(Cancel As Integer)


   Dim ctl As Control

   On Error GoTo Err_BeforeUpdate


   If Me.Dirty Then

      If MsgBox("Do you want to save?", vbYesNo + vbQuestion, _
              "Save Record") = vbNo Then
         Me.Undo
      End If
   End If

Exit_BeforeUpdate:
   Exit Sub

Err_BeforeUpdate:
   MsgBox Err.Number & " " & Err.Description
   Resume Exit_BeforeUpdate
End Sub

我还想在上面添加另一个消息框,询问用户是否要“将记录复制到分析和支持表?”。如果是,那么我希望代码运行名为“Insert_Query”的已保存查询。如果不是,则转到新记录。

我在编写 VBA 方面经验很少,因此需要帮助。

任何人都可以帮忙吗?

4

1 回答 1

2

如果您希望消息仅在他们同意保存记录时出现,然后

If MsgBox("Do you want to save?", vbYesNo + vbQuestion, "Save Record") = vbNo Then
    Me.Undo
Else
    If MsgBox("Copy the record to the Analysis & Support Table?", vbYesNo + vbQuestion, "Copy Record") = vbYes Then
        CurrentDb.Execute("Insert_Query")
    Else
        'go to new record
    end if
End If

如果您希望无论选择第一个消息框如何都显示该消息,那么

If MsgBox("Do you want to save?", vbYesNo + vbQuestion, "Save Record") = vbNo Then
    Me.Undo
End If
If MsgBox("Copy the record to the Analysis & Support Table?", vbYesNo + vbQuestion, "Copy Record") = vbYes Then
    CurrentDb.Execute("Insert_Query")
Else
    'go to new record
end if
于 2013-07-08T19:30:35.053 回答