Solved Refering to controls in subform (1 Viewer)

mafhobb

Registered User.
Local time
Today, 08:47
Joined
Feb 28, 2006
Messages
1,129
I have a main form (frmPropertyHistory) and a subform (frmMaterialsUseSubform). In the main form I have a combobox and an afterupdate event. The goal is to lock all controls in the subform when the combobox is updated.

This is my code:
Code:
    For Each ctl In Forms!frmPropertyHistory!frmMaterialsUseSubform.Controls
        Select Case ctl.ControlType                                                 'Select the controls to lock
            Case acTextBox                                                          'Textboxes
                ctl.Enabled = False
            Case acComboBox                                                         'ComboBoxes
                ctl.Enabled = False
            Case Else                                                               'Rest
                ctl.Enabled = False
        End Select
    Next

I get error 2465 in the first line: "Can't find the field "frmMaterialsUseSubform" referred to in your expression.

What am I doing wrong?

Thanks

Mafhobb
 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:47
Joined
Feb 19, 2013
Messages
12,906
if your code is in the main form you refer to the controls in the subform as

subformname.form

e.g.

For Each ctl In frmMaterialsUseSubform.Form

note that not all controls have an enabled property (labels for example) so your code will fail if you do not exclude them
 

mafhobb

Registered User.
Local time
Today, 08:47
Joined
Feb 28, 2006
Messages
1,129
Like this:?

Code:
For Each ctl In frmMaterialsUseSubform.Form

Now I get "Error 424: Object Required" at that line

Thanks for the comment on the labels and such. I'll exclude them.
 
Last edited:

arnelgp

error reading drive A:
Local time
Today, 22:47
Joined
May 7, 2009
Messages
11,536
Code:
    Dim ctl As Control
    For Each ctl In Me!frmMaterialsUseSubform.Controls
        Select Case ctl.ControlType                                                 'Select the controls to lock
            Case acTextBox, acCombobox                                                           'Textboxes
                ctl.Enabled = False
        End Select
    Next
 

mafhobb

Registered User.
Local time
Today, 08:47
Joined
Feb 28, 2006
Messages
1,129
Code:
    Dim ctl As Control
    For Each ctl In Me!frmMaterialsUseSubform.Controls
        Select Case ctl.ControlType                                                 'Select the controls to lock
            Case acTextBox, acCombobox                                                           'Textboxes
                ctl.Enabled = False
        End Select
    Next
Hi arnelgp

This returns "error 2465. This db cannot find the field "frmMaterials........."
 

arnelgp

error reading drive A:
Local time
Today, 22:47
Joined
May 7, 2009
Messages
11,536
go in design view of your form.
click on the subform and see on the Property Sheet its Name.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:47
Joined
Jul 9, 2003
Messages
12,458
Subforms are tricky because they are not actually on your form, they sit inside a window, a special control called a subform/subreport control.

So your code needs to refer to that subform/subreport control and then refer to the form that exists within it. But you don't call the form within the subform/ subreport by name, you just refer to it as "Form".

Now, Microsoft in its infinite wisdom decided that when a subform window, (a subform/subreport control) is created, by default, it takes the name of the form that exists within it. I think you can appreciate that having the subform/subreport control named the same name as the form within it can cause confusion!

Personally, I would put the code that operates on the subform controls actually in the subform, in a function and call that function from the main form.

This approach reduces the amount of code you have in the main form, which can be considerable. If you can utilise the subform separately as a stand-alone form then you have some useful code already contained in it.

I have several examples of subforms on my website.

This example demonstrates how to split a single data sheet up into to several data sheets so that you can display more records in a logical manner on your form:-

 

CJ_London

Super Moderator
Staff member
Local time
Today, 14:47
Joined
Feb 19, 2013
Messages
12,906
agreed but me. is early bound (and uses intellisense) so typos are identified on compilation. me! is late bound so typos are not identified until runtime.
 

Users who are viewing this thread

Top Bottom