以对话停止代码的形式访问打开表单 运行

Access Open Form as Dialog stopping code running

我有几个实例想要在对话框中打开一个表单并自动填写一个字段。例如:

AddClients 和 AddClientContacts 我有一个用于 AddClient 和 AddClientContact

的对话框

在 AddClient 对话框中,我想要一个按钮来打开 AddClientContact 对话框并自动填写 ID 字段。

我的代码可以打开,我的代码可以复制数据,但是打开后的代码只有在对话框关闭后才能工作。 (在 VBA 编辑器中你可以看到它仍然是 运行ning)

我试过通过 Macro OpenForm 然后通过 RunCode 和 VBA DoCmd.OpenForm 但每次都有同样的问题。

这是正常的对话行为吗?有没有办法在打开对话框命令后生成代码 运行?

只是在寻找一种打开然后填充字段的简单方法。这是我目前的VBA:

Private Sub btn_AddClientContacts_Click()
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenForm "frm_ADDClientContact", acNormal, , , acFormAdd, acDialog
Forms!frm_ADDClientContact!FK_CC_Client_ID = Forms!frm_ADDClients!Client_ID
End Sub

谢谢,KAL

Is this normal dialog behavior?

确实如此。

将您的后续代码移至对话窗体的 OnOpen 事件或从那里调用它。

不要停止来自 运行 的代码,它既浪费又危险。使用事件系统接口。

我是此代码的原作者,版权声明为您的使用自由。

Option Compare Database

' VBA Access/Excel 2016 Class: Form Instance Callback System
' Class Name: IDialogConnection

' Purpose: Create Dialogs/Pop-ups, from form instances,
'          and monitor lifecycle and process the dialog's data
'          with event callbacks (rather than waiting/sleeping).

' USAGE:

' (1) Create an instance of this class within your callee form.
' Public CallbackConnection as New IDialogConnection

' (2) Use the CallbackConnection.Methods in your callee form,
' to notify the caller of your hide/show/action operations through
' events:
'
' Public Sub Show()
'   CallbackConnection.NotifyShow
' End Sub
'
' Private Sub HideButton_Click()
'   CallbackConnection.NotifyHide
' End Sub
'
' Private Sub ActionButton_Click()
'   CallbackConnection.NotifyAction(0, SomeFormData)
' End Sub
'
' You can have as many actions as you want, and you can modify
' The data: 'SomeFormData' from within the event handler.

' (3) Create an instance of your callee form in the caller's form.
' Dim Callee as new Form_*?*

' (4) Create an Event Hook Handler in your caller's form.
' Public WithEvents DialogConnection as IDialogConnection

' (5) Connect the DialogConnection Events to your caller's form,
' the same way you Connect to other form/class events.

' (6) In the Sub Form_Load() of your caller, establish the connection:
' Set DialogConnection = Callee.CallbackConnection

Public Event OnShow()
Public Event OnHide()

Public Event OnAction(id As Integer, ByRef data As Variant)

Public Sub NotifyShow()
    RaiseEvent OnShow
End Sub

Public Sub NotifyHide()
    RaiseEvent OnHide
End Sub

Public Sub NotifyAction(id As Integer, ByRef data As Variant)
    RaiseEvent OnAction(id, data)
End Sub