Solved Cannot find Referenced Form (1 Viewer)

Rondawg65

New member
Local time
Yesterday, 20:00
Joined
Jun 14, 2024
Messages
3
I have a primary form with data fields, buttons and a subform. Data is displayed in as a datagrid in the subform, when a row is selected its associated data is displayed in the data fields of the primary form....all is working properly...The problem is when I select a button to open another form to print the data as a completed form, I get a 2450 runtime error, saying that it cannot find my prntForm....Any ideas?
 
Carefully check if you spelled the name of the form correctly.
 
Carefully check if you spelled the name of the form correctly.
Affirmative, the code calls the correct Form Name

Private Sub btnPrint_CR_Click()
Dim thisForm As Form
Set thisForm = Forms("frmCRs").Form

DoCmd.OpenForm (thisForm), acNormal
End Sub
 
The problem is when I select a button to open another form to print the data as a completed form, I get a 2450 runtime error, saying that it cannot find my prntForm....Any ideas?
You should use a report for printing
 
Code:
Set thisForm = Forms("frmCRs").Form

try
Code:
Set thisForm = Forms("frmCRs")

Why set a form variable?
 
Have you tried DoCmd.OpenForm "frmCRs" to see it that works? As Moke as implied, why even set a form variable if all you are doing is opening it?
 
This is what just solved my problem!!
Glad it worked, this is the usual way to open a form. But I feel compelled to ask, where did you find that method to open a form, Google search? If so, please share the link so we can see what is out there...
 
I'm going to guess that your Forms(...).Form attempt failed because what is returned from Forms(x) IS a form. That is, the Forms collection is a collection of individual forms. Forms(x) syntax selects one of the forms that is a member of the collection. The use of the extra .Form qualifier would fail because a form doesn't contain a form directly. Forms contain controls. Now, a subform control can contain a form, but there is a different syntax for that latter case.
 
That is, the Forms collection is a collection of individual forms. Forms(x) syntax selects one of the forms that is a member of the collection. The use of the extra .Form qualifier would fail because a form doesn't contain a form directly. Forms contain controls
That is definitely not why it failed. It simply failed because the forms collection only holds references to Open forms not all forms. So since it is not open you cannot get a reference from the Forms collection.

If the OP simply opened the form first the code works fine

Code:
Private Sub btnPrint_CR_Click()
Dim thisForm As Form
DoCmd.OpenForm ("frmCRs"), acNormal
Set thisForm = Forms("frmCRs").Form
End Sub

A form definitely has a form property which simply returns a reference to itself. It does not really do anything since it is redundant.
 
There is a second way to open a form without using the DOCMD, but it is harder to use and can have problems.

Code:
Private frm As Form
Private Sub Command0_Click()
  Set frm = New Form_Form2 ' This actually causes the form to open hidden and in design mode but needs to have a module
  'You can set a property before opening
  frm.Label0.Caption = "Caption set before visible"
  frm.Visible = True 'need to make visible
  MsgBox frm.Name
End Sub

1. You have to set a variable to persist the form. If you do not set a variable, the form would open and then close. I do not fully understand why it does this. So in this case I made frm a module level variable. If frm was local to the method the form would close immeidately once the procedure ends.
2. You have to make it visible
3. This method can run into problems if there is code in the forms opening events which will actually execute before you make the form visible.
4. This is how you open multiple instances of the same form because you cannot open multiple instances through the docmd.
 
So since it is not open you cannot get a reference from the Forms collection.

You are probably right but I'm not wrong that .Form is not a property of a form. It would fail twice due to your explanation AND mine. Even if the form WAS open, it still doesn't have a .Form property.
 
I do not understand. I explained clearly that a form has a form property and demoed this in working code. What else I have to do?
 
I had to go find a DB of mine that had sub-forms in order to explore this using the locals window. When you use .Form in a sub-form control, it refers to the contained form. When you use .Form in a "Me." context, it is self-referential to the form associated with Me. So I stand corrected.
 
Easy enough test
form.png
 
Easy enough test

I wanted a form with a subform to trace it back a layer or two and compare the deeper contents of the two cases - Me.Form and Me.subformcontrol.Form. However, the test implies that the following abomination would work:

Me.Form.Form.Form. as equivalent to simply Me. - and that makes me wonder how far the implied recursion would go. I'll say it went far enough that I rather thought of it as an abomination. Just one man's opinion about something that SEEMS excessive.
 

Users who are viewing this thread

Back
Top Bottom