【问题标题】:Access: Trigger a macro when switching recordsAccess:切换记录时触发宏
【发布时间】:2020-12-09 15:11:57
【问题描述】:

在我的 Access 表单中,我有一个宏,用于确认表单已正确填写。如果用户点击另一条记录,宏应该运行,如果需要,弹出一个消息框说“你想留在这个页面上吗?”。如果“否”,则继续下一条记录,如果“是”,则保留在同一记录上。无论哪种方式,用户所做的任何更改都会被保留。

我不确定我应该将此宏绑定到哪个表单事件,以及是否应该使用 CancelEvent 来保持相同的记录。

AfterUpdate 似乎最接近,但无论我按是还是否,CancelEvent 都会将它保留在同一记录中。

(如果宏运行 - 或不运行 - 当用户没有更改任何内容(例如在扫描记录时)时,也没关系。以更容易的为准。)

PS:我想继续使用这个宏,但如果我必须使用 VBA,我可能会使用这个解决方案:Run VBA on record change and not form open Access 2013

【问题讨论】:

  • AfterUpdate 没有取消。使用 BeforeUpdate 事件来验证记录数据。 VBA 或宏。 stackoverflow.com/questions/27713337/…
  • CancelEvent 是一个宏命令。你是说它不能取消 AfterUpdate 事件?
  • 假设我使用更新前。无论用户选择是或否,我如何更新记录?如果选择“是”,如何让当前记录保持当前记录?

标签: forms ms-access


【解决方案1】:

这就是您如何使用表单BeforeUpdate 事件与 VBA(参见代码中的 cmets):

Private Sub Form_BeforeUpdate(Cancel As Integer)
    'If no pending edits were made in the current record,
    'let the user move to another record.
    If Not Me.Dirty Then Exit Sub

    'You can check the user entries here, and if all is fine
    'just exit the sub, so the user can move to another record
    '(pending edits will be saved automatically).
    'This is just an example:
    If Me.Field1 = "a" And Me.Field2 = "b" Then Exit Sub

    'If the user doesn't want to stay on the current record,
    'let him move to another record (pending edits will be saved automatically).
    If MsgBox("Do you want to remain on this page?", vbYesNo + vbDefaultButton1, _
        "Attention") = vbNo Then Exit Sub

    'Cancel moving to another record.
    Cancel = True
End Sub

Me 在 VBA 中用于表单代码指向当前表单实例。

【讨论】:

  • 如果处理一直到取消,用户所做的更改会被撤消还是保存?
  • 此时的状态与用户不会尝试移动到另一条记录相同:任何待处理的编辑仍然存在且未保存。然后用户可以/必须保存编辑或撤消它们。
  • 我真的在寻找宏(非 VBA)解决方案,因为我已经编写了宏,但是您让我意识到我的宏在哪里编码错误。现在它在 BeforeUpdate 事件中工作。所以谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多