Solved

Looping through text boxes

Posted on 2008-10-16
4
391 Views
Last Modified: 2008-10-23
I have a form with 26 text boxes.  I'm loading default values into the text boxes from a sheet. Instead of doing something like this:

frmMyForm.tbParam1 = Cells(1,1).Value
frmMyForm.tbParam2 = Cells(1,2).Value
....... up to 26

I'd like to setup a for loop to do the work
For x = 0 to 26
 frmMyForm.tbParamx = Cells(1,x).Value
Next x

The above code of course doesn't work but that's the idea.

Is there a way to cycle through the text boxes on a form instead of calling out each independently?

Thanks!
0
Comment
Question by:JohnSantaFe
  • 2
4 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 22736768
if your tbParam name is consistent that you could do this

Cheers

Dave
For Each ccont In frmMyForm.Controls

        If TypeName(ccont) = "TextBox" Then ccont.Value = ActiveSheet.Cells(1, CInt(Replace(ccont.Name, "tbParam", vbNullString)))

    Next ccont

Open in new window

0
 
LVL 2

Accepted Solution

by:
MasterPage earned 500 total points
ID: 22737615
I would declare an array of TextBoxes that is populated in the form constructor.  Then you can loop though the array to do your business.

Note the TextBox array index is zero-based, hence the x-1.  I kept your Cells column reference one-based per your example.
Public Class frmMyForm
 

    Protected tbArray() As TextBox
 

    Public Sub New()

        ' This call is required by the Windows Form Designer.

        InitializeComponent()
 

        tbArray = New TextBox() {tbParam1, tbParam2, ...,  tbParam26 }

    End Sub
 

    Protected Sub PopulateTextBoxes()

        For x As Integer = 1 To 26

            tbArray(x - 1).Text = Cells(1, x).Value

        Next x

    End Sub
 
 

End Class

Open in new window

0
 

Author Comment

by:JohnSantaFe
ID: 22741891
Hi Dave,

What type is ccont in your example?

When I try it I get a run time type mismatch no matter if I declare ccont as an Object, Varient, or Text Box.

Thanks.
0
 

Author Comment

by:JohnSantaFe
ID: 22788743
I ended up using Master Page's idea of an array of text boxes although in a simpler way that didn't involve creating a class
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

895 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

12 Experts available now in Live!

Get 1:1 Help Now