Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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

0
skillilea
Asked:
skillilea
  • 4
  • 3
1 Solution
 
skillileaAuthor Commented:
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
0
 
peter57rCommented:
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.
0
 
peter57rCommented:
my response was to your first post.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
skillileaAuthor Commented:
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

0
 
peter57rCommented:
Are you using bound forms? (Forms have a recordsource)
0
 
skillileaAuthor Commented:
nope...this is an unbound form
0
 
peter57rCommented:
I suggest that to indicate that a value is required you use the Tag property of the control.
Set it to say, R and then you can test each control to see if the tag is R and if so whether there is a value.

This code loops through all the controls in the form(or subform) that contains the code and prints the value of any control with an R.

To create the same for the main form then repeat the code using me.parent.controls instead of me.controls

Private Sub Command8_Click()  ' choose your own event
Dim ctl As Control
On Error Resume Next
For Each ctl In Me.Controls
  If ctl.Tag = "R" Then
    Debug.Print ctl.Name, ctl.Value   'do your value test here
  Else
  End If
Next ctl
End Sub
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now