Last Update: 2022 - 10 - 29
How to check if an Access Form is open in dialog mode
by Philipp Stiefel, originally published March 23rd, 2017
Recently there was an interesting question on the MS-Office-Forum (German language) on how to check whether an Access Form is opened as Dialog or opened normally.
You open a form as dialog by setting the WindowMode-Argument of the DoCmd.OpenForm-Method to acWindowMode.acDialog. This will make the form application modal and it will stop the execution of the calling VBA code until that form is closed again.
If you have got one form that will be opened either with one WindowMode or the other depending on the action the user performs in your application, you might want to adjust the behaviour or the layout of the form to the current state of the form.
Now, in Access there is no built-in feature to determine whether the form’s current WindowMode is dialog or not.
Windows API to the rescue!
Once more the solution is to invoke the Windows API. You can use the GetWindowLong-Function to retrieve information about the current state of any window. In this specific case you need to check the WS_EX_DLGMODALFRAME property of the extended window styles to find out whether the form currently is a dialog.
I posted this short function as solution to the problem.
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongPtr, ByVal nIndex As Long) As Long Private Const GWL_EXSTYLE As Long = -20 Private Const WS_EX_DLGMODALFRAME As Long = &H1 Public Function IsFormDialog(frm As Form) As Boolean Dim hWnd As LongPtr Dim lngStyle As Long hWnd = frm.hWnd lngStyle = GetWindowLong(hWnd, GWL_EXSTYLE) IsFormDialog = CBool((lngStyle And WS_EX_DLGMODALFRAME) = WS_EX_DLGMODALFRAME) End Function
You paste the VBA-Code into a normal module or into the module of the form to use this function. This call has to originate from the code of the form itself, as the code opening the form will be suspended until the form is closed.
The function IsFormDialog expects the form you want to check as an argument. It will then invoke the GetWindowLong-API-Function for the Forms window handle (hWnd) to retrieve the extended style (GWL_EXSTYLE) and will then check whether the WS_EX_DLGMODALFRAME is set.
I declared the hwnd argument/variable as LongPtr to support the 64-bit-edition of Access as well. This works with Access 2010 and newer in the 32-bit-edition and the 64-bit-edition. If you still use an older version of Access, you should change the type of this argument/variable to a simple Long.
I will never share your email with anyone. You can unsubscribe any time.
© 1999 - 2022 by Philipp Stiefel - Privacy Policiy