Passing arguments to Access Forms created with ‘New’

calendar_today Asked Oct 24, 2014
thumb_up 9 upvotes
history Updated April 16, 2026

Question posted 2014 · +3 upvotes

I have a form called ‘detail’ which shows a detailed view of a selected record. The record is selected from a different form called ‘search’. Because I want to be able to open multiple instances of ‘detail’, each showing details of a different record, I used the following code:

Public detailCollection As New Collection    

Function openDetail(patID As Integer, pName As String)
'Purpose:    Open an independent instance of form
Dim frm As Form
Debug.Print "ID: " & patID

'Open a new instance, show it, and set a caption.
Set frm = New Form_detail
frm.Visible = True
frm.Caption = pName


detailCollection.Add Item:=frm, Key:=CStr(frm.Hwnd)

Set frm = Nothing
End Function

PatID is the Primary Key of the record I wish to show in this new instance of ‘detail.’ The debug print line prints out the correct PatID, so i have it available. How do I pass it to this new instance of the form?

I tried to set the OpenArgs of the new form, but I get an error stating that OpenArgs is read only. After researching, OpenArgs can only be set by DoCmd (which won’t work, because then I don’t get independent instances of the form). I can find no documentation on the allowable parameters when creating a Form object. Apparently, Microsoft doesn’t consider a Constructor to be a Method, at least according to the docs. How should I handle this? (plz don’t tell me to set it to an invisible text box or something) Thanks guys, you guys are the best on the net at answering these questions for me. I love you all!

Source Code for the multi-instance form taken from: http://allenbrowne.com/ser-35.html

Accepted answer +9 upvotes

Inside your Form_detail, create a custom property.

Private mItemId As Long

Property Let ItemID(value as Long)
    mItemId = value
    ' some code to re query Me
End Property

Property Get ItemId() As Long
    ItemId = mItemId
End Property

Then, in the code that creates the form, you can do this.

Set frm = New Form_detail

frm.ItemId = patId

frm.Visible = True
frm.Caption = pName

This will allow you to pass an ID to the new form instance, and ensure it gets requeried before making it visible. No need to load all of the results every time if you’re always opening the form by Newing it. You let the property load the data instead of the traditional Form_Load event.

This works because Access Form modules are nothing more than glorified classes. Hope this helps.

2 code variants in this answer

  • Variant 1 — 10 lines, starts with Private mItemId As Long
  • Variant 2 — 6 lines, starts with Set frm = New Form_detail

Top access-vba Q&A (6)

+9 upvotes ranks this answer #7 out of 12 access-vba solutions on this site .