How can I create a new instance of a form class and then open it as a acDialog box (pausing other code)?
-
26-10-2019 - |
Question
Problem
I have a complex search form that I need to open several times (at the same time) to allow users to do multiple searches at the same time. I have found the ability to open an instance of the form (as it is a class) using:
Option Compare Database
Option Explicit
'Array to hold Form Instances
Private MyFormCollection(1 To 4) As Form_frmTest
Private Sub cmd_CloneMe_Click()
Dim intCounter As Integer
For intCounter = 1 To 4
Set MyFormCollection(intCounter) = New Form_frmTest
MyFormCollection(intCounter).Caption = "Form #" & intCounter
MyFormCollection(intCounter).Visible = True
Next intCounter
Forms!frmTest.SetFocus
End Sub
This is an example to open multiple instances of a form and not what I will use for the final code as I will only need to open one instant per click BUT will likely have other already open!
BUT I can't work out a way to one of those duplicates as an acDialog (so calling function pauses until you hide or close the new dialog form). Here is the way I would normally open a normal form (not a copy):
Call DoCmd.OpenForm("SomeForm", acNormal, , , , acDialog)
Question
How can I open a new instance of a form as an Dialog box (so calling function pauses until new form is closed or hidden)?
Thanks
Solution
You can use this code to check if a form is visible:
Private Function IsVisible(intObjType As Integer, strObjName As String) As Boolean
Dim intObjState As Integer
intObjState = SysCmd(acSysCmdGetObjectState, intObjType, strObjName)
IsVisible = intObjState And acObjStateOpen
End Function
If yes, do nothing but wait:
Do While IsVisible(acForm, "frmInfo")
DoEvents
Loop
==================
Ok, another idea: you either have the choice of opening a dialog modal and stop the code in the calling function, or live with more dialogs and have the calling function running.
I think the only way of solving this is a counter of the dialogs open, and refusing to do anything in the calling function when the count is still > 0.
OTHER TIPS
You can write a public function in your called form and until that function returns (by closing the form) the calling method is blocked. In the example below clicking a "Done" button might set the return
Option Explicit
Private m_dlgResult As VbMsgBoxResult
Public Function ShowDialog() As VbMsgBoxResult
m_dlgResult = vbCancel
Me.Show vbModal
ShowDialog = m_dlgResult
End Function
Private Sub Done_Click()
m_dlgResult = vbOK
Unload Me
End Sub