VBA to open specific form/report based on combo box selection (1 Viewer)

TB11

Member
Local time
Today, 01:54
Joined
Jul 7, 2020
Messages
78
Hi.
I would like to select a specific form or report, based on a combo box and then have that form or report opened on a click event.

I have a FormName table that lists forms and reports for that a user can do data entry or view the form/report.

I have a FormMain that has cboFormName combo box. Combo box: IDFormName, FormNameForUser, FormName. The FormName is the actual form name.

On FormMain, there is a FormToOpen text box, to which the value is set as: =[cboFormName].[Column](2)

My question: how do I use a command button to open the form or report displayed in the FormToOpen text box?

I appreciate any help.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 07:54
Joined
Feb 19, 2013
Messages
16,616
docmd.openform [cboFormName].[Column](2)

or

docmd.openreport [cboFormName].[Column](2)

you need some basis to determine which - perhaps separate combo's for forms and reports or an additional column in the combo rowsource to indicate the type
 

TB11

Member
Local time
Today, 01:54
Joined
Jul 7, 2020
Messages
78
@CJ_London Thank you! Appreciate the help.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 14:54
Joined
May 7, 2009
Messages
19,245
you should add another field (ObjType, string) to your FormName table. "F" for form, "R" for report.
add this column your combo:

SELECT CASE [cboFormName].Column(3)
Case "R"
Docmd.OpenReport [cboFormName].Column(2), acViewPreview
Case "F"
Docmd.OpenForm [cboFormName].Column(2)
END SELECT
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:54
Joined
Sep 21, 2011
Messages
14,310
I had a form for each and then populated a combo with

Code:
SELECT tblObject.ReportID, tblObject.ObjectDescription, tblObject.ObjectName, tblObject.ObjectFormName, tblObject.ObjectWhere, tblObject.ObjectType
FROM tblObject
WHERE (((tblObject.ObjectType)=[TempVars]![ObjectType]))
ORDER BY tblObject.ObjectDescription;

I did use a common form for all of them though in the Load event of the form
Code:
Private Sub Form_Load()
Dim strSQL As String
' Object type gets passed in as OpenArgs
If IsNull(Me.OpenArgs) Then
    MsgBox "Missing parameter for form"
    DoCmd.Close
End If
strSQL = "SELECT * from tblObject WHERE ObjectType = '" & Me.OpenArgs & "'"
Me.RecordSource = strSQL
Me.Auto_Title0.Caption = " Open " & Me.OpenArgs
Me.txtObjectType = Me.OpenArgs
If Me.OpenArgs <> "Report" Then
    Me.chkPreview.Visible = False
    Me.lblPreview.Visible = False
End If
TempVars("ObjectType") = Me.OpenArgs
End Sub

Plus as I was using an Access switchboard, I used a few macroes
1675071285910.png

1675071188446.png
 

Users who are viewing this thread

Top Bottom