Sometimes in Microsoft Access Visual Basic you will want to know whether a form is open or not. This common requirement turns out to be a bit more fiddly than you might expect.
My original version of this function also returned True if the form was open but hidden or open in Design View. This caused problems during the development of applications. This version presented here gets around that problem with a slightly more complex check and use of the versatile, if extremely unfriendly, SysCmd function followed up by checking the CurrentView property available through the AllForms collection (as opposed to the Forms collection).
- Save this code into a new module called FormHelper.
- Call FormHelper.FormOpen and supply the name of the form whose open status you are querying.
The code is fully commented and contains more details on implementation and what the functions can and can’t do.
Option Compare Database Option Explicit Private Const Namespace$ = "FormHelper" ''' <summary> ''' Returns True if the form is open in Form View, False if not ''' </summary> ''' <param name="FormName">The name of the form whose open status we are querying.</param> ''' <remarks> ''' <para>The SysCmd method is used in conjunction with the AllForms.CurrentView method ''' so as to prevent the situation that can occur during development whereby my previous ''' FormOpen function would return True when the form is open in Design View. ''' </para> Public Function FormOpen( _ FormName As String) _ As Boolean ' Initialize error handling On Error Resume Next ' Retrieve object state of form sFrmName Dim vResult As Variant vResult = SysCmd(acSysCmdGetObjectState, acForm, FormName) If vResult And acObjStateOpen Then ' Only set to True if the form is in Form View (not Design View) FormOpen = (CurrentProject.AllForms(FormName).CurrentView = acCurViewFormBrowse) End If End Function
You can also download FormHelper.bas. This is always the latest complete version of this module and contains more functions than detailed here. The functions are fully commented.
This code was written in Visual Basic for Applications (VB, VBA) for Microsoft Access 2002 (XP) on Windows XP.