Solved

Looping through text boxes

Posted on 2008-10-16
4
390 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

747 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

11 Experts available now in Live!

Get 1:1 Help Now