"Enter parameter value" with multiple form instances

Z

zackelan

I have a document library application that has one main form and a
drill-down detail form. I need to be able to open the detail form from
two separate locations:
1. Select a record in the subform of the main form, click "View
details" command button
2. Double-click a line in the "related documents" listbox of the detail
form to open that document's detail view.

Since method #2 requires having multiple instances of the same form
open, I'm using a modified version of Allen Browne's OpenAClient
procedure (http://allenbrowne.com/ser-35.html):

Global gclnForms As New Collection
Sub OpenNewDetailForm(DocID As String)

Dim frm As Form

'Open a new instance, show it, and set a caption.
Set frm = New Form_frmDocument
frm.Visible = True
frm.Filter = "DocID = '" & DocID & "'"
frm.FilterOn = True
frm.Caption = "Document " & DocID

'Append it to our collection.
gclnForms.Add Item:=frm, Key:=CStr(frm.Hwnd)

Set frm = Nothing
End Sub

Here, frmDocument is the detail form, and DocID is the primary key of
the table of documents. I'm calling this code from my main form:

Private Sub cmdDetail_Click()
OpenNewDetailForm Form_sfrmMain.DocID
End Sub

And from the detail form itself:

Private Sub lstReferencedDocs_DblClick(Cancel As Integer)
OpenNewDetailForm Me.lstReferencedDocs
End Sub

When either of the code snippets above is run, I get an "Enter
parameter value" prompt asking for Forms!frmDocument!DocID. When I
trace the code, this prompt comes up immediately after running the "Set
frm = New Form_frmDocument" line in OpenNewDetailForm. I can hit either
OK (with or without something in the input box) or Cancel at the prompt
and the form loads as it should. The prompt doesn't come up at all if I
do a DoCmd.OpenForm "frmDocument".

I'd appreciate any help.

Zack
 
A

Allen Browne

It sounds like this form is based on a query, and the query has this in the
Criteria row:
Forms!frmDocument!DocID

You will not be able to use that approach for multiple instances. You will
need to find another way to pass the literal value the form that does not
rely on the form name, since the form name is not unique when you have
mulitple instances of the same form open.

Another way to pass the filter string would be via a public variable. In the
General Declarations section (top) of a standard module (created by clicking
New on the Modules tab of the Database window):
Public gstrFormFilter As String

Before you open your instance:
If Not IsNull(Me.DocID) Then
gstrFormFilter = "DocID = """ & DocID & """"
End If

In the Open event procedure of frmDocument, you can then apply the filter to
the correct instance like this:
If gstrFormFilter <> vbNullString Then
Me.Filter = gstrFormFilter
Me.FilterOn = True
gstrFormFilter = vbNullString
End If

BTW, the code does need to check for a valid value of DocID in the original
form. Also, if DocID is a Number field (not a Text field), drop the extra
quotes, i.e.:
gstrFormFilter = "DocID = " & DocID
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top