How to loop through controls with VBA

I have a form with the following text boxes:

txtOrderNo1     txtItemNo1     txtTrxQty1     txtComment1
txtOrderNo2     txtItemNo2     txtTrxQty2     txtComment2
txtOrderNo3     txtItemNo3     txtTrxQty3     txtComment3
...
...
txtOrderNo10   txtItemNo10    txtTrxQty10   txtComment10

I need to loop through these controls and process each line (OrderNo, ItemNo, TrxQty, & Comment).  However, this is with VBA so I cannot setup an array of controls and there is no Index property.  I would like some method of looping through these controls without copying/pasting my code 10 times.  

Let me know if you need any more information.  Thanks.

~bruno71



bruno71Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vinnyd79Commented:
Have you tried looping through the controls collection.

Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is TextBox Then
    MsgBox Ctrl.Name
End If
Next Ctrl
0
Mikal613Commented:
Vinny got this one
0
Mikal613Commented:
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

EDDYKTCommented:
VBA?
0
bruno71Author Commented:
I can loop through the controls, but I need to use all 4 items at the same time.  Example:

- Search a table by txtItemNo1, update a field by adding txtTrxQty1
- Open another table, add a new record with txtOrderNo1, txtItemNo1, and txtComment1
- Repeat with txtItemNo2, txtTrxQty2, txtOrderNo2, and txtComment2

How can I grab the corresponding OrderNo, ItemNo, TrxQty, & Comment by looping through the controls?

~bruno71
0
RogueSolutionsCommented:
You can access a control by its name as well as its id.  So ...

Me.Controls("txtItemNo" & iEntry) will get the the control with the name txtItemNo1 if iEntry is a 1

You can loop iEntry and work with the various controls that way.  At least I think that's what you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GindleCommented:
I had to do this for a college project recently. I assume you have a fair idea of how to use vba. Therefore I shall post the Sub Routine I created, and how i used it.

<<<<<<<SUB ROUTINE>>>>>>>

Private Sub UpdateControls(ByVal Control As String, ByVal Index As Integer, ByVal RecordPosition As Integer)
    Dim dbMyConnection As ADODB.Connection
    Dim rcdTableBooked As New ADODB.Recordset
   
    Set dbMyConnection = CurrentProject.Connection
    rcdTableBooked.Open "TablesBooked", dbMyConnection, adOpenKeyset, adLockOptimistic, adCmdTable
   
    rcdTableBooked.AbsolutePosition = RecordPosition
   
    Dim x As Integer
    Dim y As Integer
   
    For x = 0 To Forms.Count - 1
        If Forms(x).Name = "Table Layout" Then
            For y = 0 To Forms(x).Controls.Count - 1
                If Forms(x).Controls(y).Name = Control & Index Then
                    Forms(x).Controls(y).Value = rcdTableBooked.Fields(Control).Value
                End If
            Next
        End If
    Next
End Sub

<<<<<<EXAMPLE ON USE>>>>>>>

    Set dbMyConnection = CurrentProject.Connection
    rcdTableBooked.Open "TablesBooked", dbMyConnection, adOpenKeyset, adLockOptimistic, adCmdTable

    rcdTableBooked.MoveFirst
    i = 0
    Do While Not rcdTableBooked.EOF
        If rcdTableBooked!TableNumber = Table Then
            UpdateControls "Title", i, rcdTableBooked.AbsolutePosition
            UpdateControls "Initials", i, rcdTableBooked.AbsolutePosition
            UpdateControls "Surname", i, rcdTableBooked.AbsolutePosition
            UpdateControls "Street", i, rcdTableBooked.AbsolutePosition
            UpdateControls "Village", i, rcdTableBooked.AbsolutePosition
            UpdateControls "Town", i, rcdTableBooked.AbsolutePosition
            UpdateControls "County", i, rcdTableBooked.AbsolutePosition
            UpdateControls "Postcode", i, rcdTableBooked.AbsolutePosition
            UpdateControls "Telephone", i, rcdTableBooked.AbsolutePosition
            i = i + 1
        End If
        rcdTableBooked.MoveNext
    Loop

Hope This Helps. Please feel free to ask if you need anymore help at all.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.