MS Access OpenForm acDialog option does not seem t

2019-06-13 22:53发布

问题:

This

DoCmd.OpenForm fnew, , , , , acDialog

Doesn't seem to stop code execution for some reason. Is it because I'm calling the function that has this method from another function and that is messing it up? E.g.

func1
<code>
Call func2
    <func2 code>
    DoCmd.OpenForm fnew, , , , , acDialog
<back to func1 code that executes even though i dont want it to until the form closes>

回答1:

With acDialog as the OpenForm WindowMode parameter, your calling code should not continue until the form is closed.

It should not matter that OpenForm takes place in another procedure called from your code. In this example, when running func1, the MsgBox is not displayed until the form closes.

Public Function func1()
    Call func2
    MsgBox "form closed"
End Function

Public Function func2()
    'DoCmd.OpenForm "Form3", , , , , acDialog
    DoCmd.OpenForm "Form3", WindowMode:=acDialog
End Function

Note that code operates as described as long as the form is not already open. If it's open in Design View, calling OpenForm only switches it to Form View without applying acDialog. If open in Form View, nothing happens, which means acDialog is not applied then either.

If you want to guarantee that acDialog is applied, make sure the form is closed before calling OpenForm ...

Public Function func2()
    Const cstrForm As String = "Form3"
    If CurrentProject.AllForms(cstrForm).IsLoaded Then
        DoCmd.Close acForm, cstrForm
    End If
    'DoCmd.OpenForm cstrForm, , , , , acDialog
    DoCmd.OpenForm cstrForm, WindowMode:=acDialog
End Function


回答2:

I struggled with this one for ages until I realised that the acDialog setting does nothing if the form is open in design view when the code is run. It does actually pause the code from the opening procedure when run in normal use (i.e. no one's trying to design anything). If you want to test this behaviour, create a form "zzTestForm", and run this from a module:

Public Sub DoThis()

MsgBox "ok, let's go"

DoCmd.OpenForm "zzTestForm", acNormal, , , acFormPropertySettings, acDialog

MsgBox "you done yet?"

End Sub