[AccessD] The Mysterious Case of the Missing Code

Kenneth Ismert kismert at gmail.com
Mon May 7 18:48:48 CDT 2012


>
> John Clark:
> ...and it is buggin' out on some pretty straight forward code. ...see
> below...
> ...
>    stDocName = "frmCriteria"
>    DoCmd.OpenForm stDocName, , , stLinkCriteria
>    Form_frmCriteria.DtStartDate.Visible = True
> ...
> *** It doesn't like the "Form_frmCriteria" part...back to the drawing
> board.
>

John,

Not surprising at all. The fact that that code worked before is entirely
coincidental.

Consider routine zTestFormNativeRef (below). Depending on the order that
you run the form create commands, you will get 2 form instances, or just 1.
Form_frmCriteria may refer to the form you opened with DoCmd, or it may
not. You just can't be sure.

A fix is to use code like this:

   Dim stDocName As String
   Dim rFrm As Form_frmCriteria

   stDocName = "frmCriteria"
   DoCmd.OpenForm stDocName, , , stLinkCriteria
   Set rFrm = Forms(stDocName)
   With rFrm
       .DtStartDate.Visible = True
       ' etc...
    End With

By retrieving the named form reference, you are guaranteed to be working
with what you intended.

This works beautifully, unless you need multiple instances of frmCriteria
open at one time. If that is the case, I really suggest dumping Access and
moving to something like .NET.

-Ken

Public Sub zTestFormNativeRef()

    Const S_FORM As String = "frmCriteria"

    Dim sOut As String
    Dim rFrm As Access.Form

    ' this order creates 2 forms
    Form_frmCriteria.Visible = True
    GoSub ShowForms
    DoCmd.OpenForm S_FORM
    GoSub ShowForms

    ' this order creates 1 form
'    DoCmd.OpenForm S_FORM
'    GoSub ShowForms
'    Form_frmCriteria.Visible = True
'    GoSub ShowForms

    Debug.Print
    Debug.Print Form_frmTest Is Forms(0)
    If Forms.Count > 1 Then
        Debug.Print Form_frmTest Is Forms(1)
    End If
    Debug.Print

    ' closes a named form - order not guaranteed
    DoCmd.Close acForm, S_FORM
    GoSub ShowForms
    DoCmd.Close acForm, S_FORM
    GoSub ShowForms

    Exit Sub

ShowForms:
    sOut = "Forms: " & Forms.Count & ":"
    For Each rFrm In Forms
        sOut = sOut & " " & rFrm.Name
    Next
    Debug.Print sOut
    Return
End Sub


More information about the AccessD mailing list