Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

iterate thru specific controls in a form using a loop

lets say i have 10 textboxes in a form named txt1 to txt10 but i would like to iterate only the txt3,txt6,txt8,txt9 and multiple their values to 2 i can type it like below but how can i do it using a loop

txt3.value = txt3.value * 2
txt6.value = txt6.value * 2
txt8.value = txt8.value * 2
txt9.value = txt9.value * 2

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First define a array and store textbox numbers you want to iterate. Then traverse that array using for loop and execute operation you want. Try below sub.

Private Sub CommandButton1_Click()
Dim arr
Dim i As Long

    arr = Array(3, 6, 8, 9)
    
    For i = LBound(arr) To UBound(arr)
        Me.Controls("TextBox" & arr(i)) = Me.Controls("TextBox" & arr(i)) * 2
    Next i

End Sub
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading