Link to home
Start Free TrialLog in
Avatar of skillilea
skillilea

asked on

How do I loop though all fields including a sub form

I have a main form and a sub form:

fields on both

I am trying to loop through each field and keep getting error.

mainform = frmMain
subform = oSubFrm


The txtInnoScan throws the error.


Private Sub cmdAsset_Add_Click()
    Dim controlArray() As Variant: controlArray = FormControls()
    Dim ctl As Variant
    
    For Each ctl In controlArray
        MsgBox Controls(ctl).Name
    Next
End Sub


Private Function FormControls() As Variant()
    FormControls = Array("txtAssetType", "Forms!frmMain.txtInnoScan")
    '"txtInnoScan",
End Function

Open in new window

Avatar of skillilea
skillilea

ASKER

the only way I  can figure it out is to do two loops

    For Each ctl In Me.Parent.Controls
        MsgBox ctl.Name    Next

    For Each ctl In Me.Controls
        MsgBox ctl.Name
    Next

how do I get all the controls on the page

tnx
Avatar of peter57r
I don't see the point of this code.
The only way it can display the name of a control is if you specify the name in advance in the array.

Can you describe what you are really trying to do and maybe someone will suggest a method.
my response was to your first post.
Sorry for the confusion.

I have 20 fields that are required prior a record entry.

I have an array of all the field names.
Some of these fields are on the parent or main form some are on a sub form.

I want to loop through each field and check for values but I can't seem to reference the mainform fields without two loops.

the code is being called from the sub form  ie.  the "Add New Button" is located on the sub form.

tnx for the help

Are you using bound forms? (Forms have a recordsource)
nope...this is an unbound form
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial