?
Solved

How do I loop though all fields including  a sub form

Posted on 2011-02-22
7
Medium Priority
?
225 Views
Last Modified: 2012-05-11
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
Comment
Question by:skillilea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:skillilea
ID: 34951803
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
 
LVL 77

Expert Comment

by:peter57r
ID: 34951838
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
 
LVL 77

Expert Comment

by:peter57r
ID: 34951844
my response was to your first post.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:skillilea
ID: 34951876
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
 
LVL 77

Expert Comment

by:peter57r
ID: 34952144
Are you using bound forms? (Forms have a recordsource)
0
 

Author Comment

by:skillilea
ID: 34952167
nope...this is an unbound form
0
 
LVL 77

Accepted Solution

by:
peter57r earned 2000 total points
ID: 34954845
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question