Solved

How do I loop though all fields including  a sub form

Posted on 2011-02-22
7
219 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
Comment Utility
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
Comment Utility
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
Comment Utility
my response was to your first post.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:skillilea
Comment Utility
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
Comment Utility
Are you using bound forms? (Forms have a recordsource)
0
 

Author Comment

by:skillilea
Comment Utility
nope...this is an unbound form
0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now