1
votes

It seems so simple, but I can't seem to find the correct syntax. I am looking to create a form variable for an unopened form in my Access 2013 application so I can pass the form object to a series of methods rather than passing separate properties of the form.

Here's what I've been attempting, but I run into Run-time error '2465' "Microsoft Access can't find the field 'frmSuppliers' referenced in your expression.

Private Sub cmdSuppliers_Click()
    Dim frm As Form
    Set frm = Form("frmSuppliers")

    Debug.Print frm.Name
2
You have to open the form for it to be initialized and added to the forms collection of the DB - Sorceri

2 Answers

4
votes
Private Sub cmdSuppliers_Click()
    Dim frm As Form
    Set frm = Forms("frmSuppliers")

    Debug.Print frm.Name

You referenced the Forms collection but made it singular instead of plural.

3
votes

Thank you Sorceri and Christopher. I seem to have been missing a compilation of the wrong reference to the forms collection and not opening the form prior to setting the variable. This code seems to do the trick:

Private Sub cmdSuppliers_Click()
    Dim frm As Form

    DoCmd.OpenForm "frmSuppliers", acNormal, , , , acHidden
    Set frm = Forms("frmSuppliers")

    Debug.Print frm.Name

    DoCmd.Close acForm, frm.Name
    Set frm = Nothing

End Sub