PDA

View Full Version : And this



Norie
05-07-2005, 10:45 AM
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
Else
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
Forms(NForm).SetFocus
DoCmd.GoToControl subForm
DoCmd.GoToControl subField
DoCmd.FindRecord subCriteria
ChangeForms = True
End If
Else
ChangeForms = True
End If

Exit_ChangeForms:
If OForm <> NForm Then
If ChangeForms Then
DoCmd.Close acForm, OForm
Else
DoCmd.Close acForm, NForm
Forms(OForm).SetFocus
End If
End If

Exit Function

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

End Function