Note that this sub must be in a module and called from an eventprocedure
on a FORM
This code is flexible enough to handle most form switching and in fact is
perfect for 'roll your own switchboards'.
Remove your code for .visible, this does it all.
For your example, a command button's click event (or other event) on the
FormOne would invoke this:
Call subSwapForm("FormTwo")
The calling form (FormOne) would become hidden, and FormTwo would either
unhide itself (if loaded) or load if not loaded.
The end result is FormOne hidden, FormTwo showing.
'------------------------------------------------------------------------
'Sub will optionally hide/close current form and load/make visible a
selected form.
'Params: GetForm - String name of form to swap to.
' ToControl - String control name to get focus in new form.
' Disp - Disposition of calling form
' "CLOSE" = Close calling form
' "KEEP" = Leave calling form unhidden
' Anything else inluding nullstring = hide calling form
'Note that this sub must be in a module and called from an eventprocedure
on a FORM.
'------------------------------------------------------------------------
Sub subSwapForm(GetForm As String, Optional ToControl As String, Optional
Disp As String)
On Error GoTo LogErr
DoCmd.SetWarnings True
Dim I As Integer, IsLoaded As Integer
IsLoaded = False
Select Case Disp
Case "CLOSE"
Dim cf As Form
Set cf = Screen.ActiveForm
DoCmd.Close acForm, cf.name
Set cf = Nothing
Case ""
Screen.ActiveForm.Visible = False
Case "KEEP"
Case Else
End Select
For I = 0 To Forms.Count - 1
If Forms(I).FormName = GetForm Then
IsLoaded = True
Exit For
End If
Next
If IsLoaded = True Then
Forms(I).Visible = True
Else
DoCmd.OpenForm GetForm
End If
If Len(ToControl) > 0 Then DoCmd.GoToControl ToControl
subSwapForm_exit:
Exit Sub
LogErr:
'Call fcnErrorLog("basDBTC Modules", "subSwapForm")
If Err = 2475 Then Resume Next
MsgBox "subSwapForm Error " & Err & " " & Error$
Resume subSwapForm_exit
End Sub
HTH, UpRider