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
Solved

How do I loop though all fields including  a sub form

Posted on 2011-02-22
7
221 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
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 500 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
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.
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…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

856 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