访问打开表单作为对话框停止运行代码

时间:2016-06-05 17:48:45

标签: vba ms-access dialog access-vba ms-access-2010

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

AddClients和AddClientContacts 我有一个AddClient和AddClientContact

的对话框

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

我的代码可以打开,我的代码可以复制数据,但打开后的代码在关闭对话框之前不起作用。 (在VBA编辑器中,你可以看到它仍在运行)

我已尝试通过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

2 个答案:

答案 0 :(得分:1)

  

这是正常的对话行为吗?

确实

将您的继续代码移至对话框的OnOpen事件或从那里调用。

答案 1 :(得分:0)

不要停止运行代码,这既浪费又危险。使用事件系统界面。

我是这段代码的原始作者,并声明您对使用这些内容的自由。

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