• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 854
  • Last Modified:

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



0
bruno71
Asked:
bruno71
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now