Solved

How do I loop though all fields including  a sub form

Posted on 2011-02-22
7
220 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
Backup Your Microsoft Windows Server®

Backup 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.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

815 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

13 Experts available now in Live!

Get 1:1 Help Now