Run-time error '2455' when reading control names from a subform. (1 Viewer)

CodeByJim

New member
Local time
Today, 11:59
Joined
Apr 18, 2015
Messages
9
Hello.

I have a procedure that collects control names from a subform into a CSV string.
I have been using procedures like this for years without any problems.

Suddenly I start getting Run-time error '2455':
"You entered an expression that has an invalid reference to the property Form/Report."

I have setup a subform object and a control object.
Dim iSubForm as SubForm
Dim Ctrl as Control
I use a constant for the subform name.
Const SFName as String = "sfrmSelectRecord"

The subform is instantiated and it's name is printed.
Set iSubForm = Forms(0).Controls(SFName)
Debug.Print iSubForm.Name

The statement that generates Error '2455' is:
For Each Ctrl In iSubForm.Controls
Debug.Print Ctrl.Name
Next

The subform object is terminated when the loop finishes.
set iSubForm = Nothing

I have tried replacing the For Each statement with:
For Each Ctrl In iSubForm.Form.Controls
The same error is raised.

I am hoping to find a solution to this short of deleting and reinstalling Office!
I suspect a Microsoft Update is causing the problem, but have no evidence.

Any help would be appreciated!

Thank you.
 
So do you actually have an object in iSubForm?
Are you able to rollback an office update?
 
Is the subform possibly in the detail section and no record is displayed in the main form?
 
I'm going to quote two lines out of context.

Code:
Set iSubForm = Forms(0).Controls(SFName)
...
For Each Ctrl In iSubForm.Controls

In this context, iSubForm is a control (a sub-form control, but a control nonetheless.) If I then write out the implied substituted qualification (string together the dotted items), that reads For Each Ctrl In Forms(0).Controls(SFName).Controls - but that means that you are looking at a collection of controls inside a collection of controls. I believe you are missing a .Form from between the first and second Controls references because to the best of my knowledge, a control doesn't contain controls - but the form inside the sub-form control DOES have its own control set. One way to fix this might be as simple as

Code:
For Each Ctrl In iSubForm.Form.Controls

I wouldn't doubt that you got pegged by an Office Update, but the question is whether they changed defaults on something because it was being too permissive, and that it could cause other errors if they didn't fix it. To know why it worked for so long would require detailed knowledge of the patches, and it is sometimes difficult to extract that kind of information from a patch description. All too often they will describe a given patch as "containing miscellaneous fixes for sub-form behavior" and leave it at that. No details, just a bag of miscellaneous fixes. I had to deal with regular MS patches when I was with the U.S. Navy and I can tell you that getting details out of MS at that level is like pulling teeth from a conscious shark.
 
The most common problem when dealing with subforms is that you are not actually dealing with a subform.

There is no such thing as a "Subform"

What it is, is a special control that surrounds a normal form, creating the impression of a form within a form.

To find the controls in the "Subform" you need to know the hierarchy.

1) - Mainform
2) - Subform/Subreport Control
3) - Form bounded/contained by the above Control

To access the controls in the contained form you refer to the container.

Your code would be something like:-

Mainform.Subform/Subreport Control.Form

Now to make matters worse, when you drag a form onto a main form to create a subform, Microsoft gives the Subform control the same name as the form you are dragging onto the main form.

Your Mainform will contain a control named:-

sfrmSelectRecord

You think it's the form, but it's not! It is the Subform/Subreport Control. Microsoft, by default, creates a subform/Subreport Control to house/contain your form and names it the same as the form that it contains.

This default behaviour of Microsoft is the source of much confusion....
 

Users who are viewing this thread

Back
Top Bottom