Solved

Looping through text boxes

Posted on 2008-10-16
4
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

695 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