[VBA] Option Compare Database
Option Explicit

Function ChangeForms(ByRef NForm As String, ByRef OForm As String, Optional frmCriteria As String, _
Optional subForm As String, Optional subField As String, Optional subCriteria As String)
' this function will change from OForm to NForm
' the optional arguments allow the following
' frmCriteria - will open OForm to record according to this criteria
' subForm, subField, subCriteria - if Nform contains a subform with records
' related to a record on OForm then if these arguments are present (and correct)
' focus will move to related record on the subform

Dim subFind As Boolean
On Error GoTo Err_ChangeForms

If frmCriteria = "" Then
DoCmd.OpenForm NForm
DoCmd.OpenForm NForm, , , frmCriteria
ChangeForms = True
GoTo Exit_ChangeForms
End If

subFind = Not (subForm = "" Or subField = "" Or subCriteria = "")

If subFind Then
If IsSubForm(NForm, subForm) Then
DoCmd.GoToControl subForm
DoCmd.GoToControl subField
DoCmd.FindRecord subCriteria
ChangeForms = True
End If
ChangeForms = True
End If

If OForm <> NForm Then
If ChangeForms Then
DoCmd.Close acForm, OForm
DoCmd.Close acForm, NForm
End If
End If

Exit Function

MsgBox Err.Description, vbInformation, "Error message"
ChangeForms = False
Resume Exit_ChangeForms

End Function