Function OpenFrmOrRptUsingCaption() As Integer '* This function will open the form or report determined '* by the active control caption. Alternately, the form '* or report name can be specified in the active control '* tag property. '* '* Ex.: A command button caption may be "Orders". When '* this function is executed from the button's OnClick '* property, the form named "frmOrders" will be opened. Dim i As Integer Dim s As String Dim ipos As Integer Dim intfoundit As Integer Dim dbs As DAO.Database Dim ctl As Control Dim frm As Form Dim rpt As Report Dim strFrmToOpen As String Dim strRptToOpen As String Dim strRptPrefix As String Dim strFrmPrefix As String On Error GoTo Err_OpenFrmOrRptUsingCaption Set dbs = CurrentDb() strRptPrefix = "rpt" strFrmPrefix = "frm" Set ctl = Screen.ActiveControl If IsNull(ctl.Tag) Or ctl.Tag = "" Then '* Remove the "&" character from the button caption, if it exists s = ctl.Caption ipos = InStr(1, s, "&") If ipos = 0 Then Else s = Mid(s, 1, ipos - 1) & Mid(s, ipos + 1, Len(s)) End If '* If the control tag property is empty, use the '* active control caption to build next form or report name strFrmToOpen = strFrmPrefix & s strRptToOpen = strRptPrefix & s Else '* Otherwise, it is assumed that the control tag '* contains the full form or report name. strFrmToOpen = ctl.Tag strRptToOpen = ctl.Tag End If '* Determine if form name is an existing form intfoundit = False For i = 0 To (dbs.Containers!Forms.Documents.Count - 1) If dbs.Containers!Forms.Documents(i).name = strFrmToOpen Then intfoundit = True Exit For End If Next i If intfoundit Then '* Form exists, so open it. Set frm = Screen.ActiveForm DoCmd.OpenForm strFrmToOpen If frm.name = "MyMainMenuFormName" Then 'Don't close this form. Else '* Un-comment the line below to close the original form. 'DoCmd.Close A_FORM, frm.name End If Else '* Determine if report name is an existing report For i = 0 To (dbs.Containers!Reports.Documents.Count - 1) If dbs.Containers!Reports.Documents(i).name = strRptToOpen Then intfoundit = True Exit For End If Next i If intfoundit Then '* Report exists, so open it. DoCmd.OpenReport strRptToOpen, acViewPreview Else '* Could not find form or report name MsgBox "A Form named '" & strFrmToOpen _ & "' or a report named '" _ & strRptToOpen & "' was not found." End If End If Exit_OpenFrmOrRptUsingCaption: Set dbs = Nothing Set ctl = Nothing Set frm = Nothing Set rpt = Nothing Exit Function Err_OpenFrmOrRptUsingCaption: If Err = 2102 Then Beep MsgBox Error$ Resume Exit_OpenFrmOrRptUsingCaption Else MsgBox Err & " " & Error$ Resume Next End If End Function