0
votes

I have several instances where I want to open a form in a dialaog and auto fill out a field. For example:

AddClients and AddClientContacts I have a dialog box for both AddClient and AddClientContact

When in the AddClient dialog I want to have a button to open the AddClientContact dialog and auto fill the ID field.

My code works to open, and my code works to copy the data but the code after opening doesn't work until dialogs are closed. (and in VBA editor you can see it's still running)

I have tried via Macro OpenForm then RunCode and VBA DoCmd.OpenForm but have the same problem each time.

Is this normal dialog behavior? Is there a way to make code run after an open dialog command?

Just looking for a simple way to open then populate a field. This is the VBA I have at the moment:

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

Thanks, KAL

2

2 Answers

1
votes

Is this normal dialog behavior?

Indeed.

Move your continuing code to OnOpen event of the dialogue form or call it from there.

0
votes

Don't stop the code from running, its wasteful and hazardous. Use an event system interface.

I am the original author of this code, with the copy-claim for your freedoms with the usages.

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