?
Solved

Looping through text boxes

Posted on 2008-10-16
4
Medium Priority
?
396 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 1500 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

765 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