471,066 Members | 1,307 Online

# populate textboxes with a loop??

Apologies in advance if this sounds slow-witted, but I didn't find it here.

Need to populate some textboxes on a form. While I could hard code it (the
number of options aren't that high), it would be more compact/efficient to
use a loop.

However, I haven't found anything that describes how to reference the
textboxes sequentially ... evaluating the hard part of the textbox name,
with the number of the loop iteration, to address the specific box.

E.G. textboxes named textbox1, textbox2, textbox3

For x = 1 to 3
{textbox(x)} = x
Next x

What I need is how to call out the part of the line {in braces} so that it
executes dynamically. In the stupid example, t-box 1 would have 1, t-box 2
2, t-box 3 3.

I really don't want to get into arrays here, would be killing an ant with a
cannon, and more learning curve to overcome ... even though my example looks
like array notation

Thanks in advance for help with a real rookie question, but I don't write
much code ... as should be painfully obvious from the question.

Kevin
Aug 21 '05 #1
4 18511
> Apologies in advance if this sounds slow-witted, but I didn't find it
here.

Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it would be more compact/efficient to use a loop.

However, I haven't found anything that describes how to reference the
textboxes sequentially ... evaluating the hard part of the textbox name, with the number of the loop iteration, to address the specific box.

E.G. textboxes named textbox1, textbox2, textbox3

For x = 1 to 3
{textbox(x)} = x
Next x

What I need is how to call out the part of the line {in braces} so that it executes dynamically. In the stupid example, t-box 1 would have 1, t-box 2 2, t-box 3 3.

I really don't want to get into arrays here, would be killing an ant with a cannon, and more learning curve to overcome ... even though my example looks like array notation

You have the wrong impression about arrays... they are extremely useful
devices. In the case of controls, VB provides a construct called control
arrays. They are quite easy to work with and will easily do what you
asked. The key to creating a control array is to give all of the member
controls (they must be of the same type) the same name. The first
duplicate you name will popup a dialog box asking if you want to create
a control array. Answer Yes to the question. Each member of the control
array will have its Index property assigned a value and they will all
share the same events. The event procedures will automatically be given
a parameter (between the parentheses) called, believe it or not, Index.
The beauty of this system is that VB will automatically assign the Index
property value to this Index parameter so that you will know which
control's event was activated. Anyway, for reference purposes, you can
address the members of the control array just like you would the members
of a normal array. For example, the question you asked would be coded
this way...

For X = 1 To 3
Text1(x).Text = CStr(X)
Next

Look up "control arrays" (without the quote marks) in VB's help files

Rick
Aug 21 '05 #2
Rick:

Guess I should have been more specific. This is in a MS access project
(form, specifically). Attempting what you outlined gives a duplicate name
error from access ... so no joy in mudville yet.

Other ways to evaluate a dynamic expression inside the code??

Kevin
"Rick Rothstein [MVP - Visual Basic]" <ri************@NOSPAMcomcast.net>
wrote in message news:i4********************@comcast.com...
Apologies in advance if this sounds slow-witted, but I didn't find it

here.

Need to populate some textboxes on a form. While I could hard code it

(the
number of options aren't that high), it would be more

compact/efficient to
use a loop.

However, I haven't found anything that describes how to reference the
textboxes sequentially ... evaluating the hard part of the textbox

name,
with the number of the loop iteration, to address the specific box.

E.G. textboxes named textbox1, textbox2, textbox3

For x = 1 to 3
{textbox(x)} = x
Next x

What I need is how to call out the part of the line {in braces} so

that it
executes dynamically. In the stupid example, t-box 1 would have 1,

t-box 2
2, t-box 3 3.

I really don't want to get into arrays here, would be killing an ant

with a
cannon, and more learning curve to overcome ... even though my example

looks
like array notation

You have the wrong impression about arrays... they are extremely useful
devices. In the case of controls, VB provides a construct called control
arrays. They are quite easy to work with and will easily do what you
asked. The key to creating a control array is to give all of the member
controls (they must be of the same type) the same name. The first
duplicate you name will popup a dialog box asking if you want to create
a control array. Answer Yes to the question. Each member of the control
array will have its Index property assigned a value and they will all
share the same events. The event procedures will automatically be given
a parameter (between the parentheses) called, believe it or not, Index.
The beauty of this system is that VB will automatically assign the Index
property value to this Index parameter so that you will know which
control's event was activated. Anyway, for reference purposes, you can
address the members of the control array just like you would the members
of a normal array. For example, the question you asked would be coded
this way...

For X = 1 To 3
Text1(x).Text = CStr(X)
Next

Look up "control arrays" (without the quote marks) in VB's help files

Rick

Aug 21 '05 #3
What I really need is how do you:
Inside code, take a text string, concatenate the value of a counter,
evaluate the result in real time, and then use the result to set a control
value. Referencing a series of access form controls sequentially ... it
shouldn't be difficult, but I'm still getting zippo for help from the VB
help files.

Thanx again

"Kevin H" <sp*****@noyoudont.com> wrote in message
news:II********************@ptd.net...
Rick:

Guess I should have been more specific. This is in a MS access project
(form, specifically). Attempting what you outlined gives a duplicate name
error from access ... so no joy in mudville yet.

Other ways to evaluate a dynamic expression inside the code??

Kevin
"Rick Rothstein [MVP - Visual Basic]" <ri************@NOSPAMcomcast.net>
wrote in message news:i4********************@comcast.com...
Apologies in advance if this sounds slow-witted, but I didn't find it

here.

Need to populate some textboxes on a form. While I could hard code it

(the
number of options aren't that high), it would be more

compact/efficient to
use a loop.

However, I haven't found anything that describes how to reference the
textboxes sequentially ... evaluating the hard part of the textbox

name,
with the number of the loop iteration, to address the specific box.

E.G. textboxes named textbox1, textbox2, textbox3

For x = 1 to 3
{textbox(x)} = x
Next x

What I need is how to call out the part of the line {in braces} so

that it
executes dynamically. In the stupid example, t-box 1 would have 1,

t-box 2
2, t-box 3 3.

I really don't want to get into arrays here, would be killing an ant

with a
cannon, and more learning curve to overcome ... even though my example

looks
like array notation

You have the wrong impression about arrays... they are extremely useful
devices. In the case of controls, VB provides a construct called control
arrays. They are quite easy to work with and will easily do what you
asked. The key to creating a control array is to give all of the member
controls (they must be of the same type) the same name. The first
duplicate you name will popup a dialog box asking if you want to create
a control array. Answer Yes to the question. Each member of the control
array will have its Index property assigned a value and they will all
share the same events. The event procedures will automatically be given
a parameter (between the parentheses) called, believe it or not, Index.
The beauty of this system is that VB will automatically assign the Index
property value to this Index parameter so that you will know which
control's event was activated. Anyway, for reference purposes, you can
address the members of the control array just like you would the members
of a normal array. For example, the question you asked would be coded
this way...

For X = 1 To 3
Text1(x).Text = CStr(X)
Next

Look up "control arrays" (without the quote marks) in VB's help files

Rick

Aug 21 '05 #4
> Guess I should have been more specific. This is in a MS access project
(form, specifically). Attempting what you outlined gives a duplicate name error from access ... so no joy in mudville yet.
Yes, you should definitely mention the type of project you are working
on. The newsgroups with "vb" in their names are almost exclusively for
the stand alone Visual Basic language. As you have seen, solutions that
work in it do not necessarily translate over to the VBA programming
modules contained within the Office product line. You might want to post
in a more relevant newsgroup. Microsoft has a public newsgroup server
that probably has a group that would be a closer fit to what you want
than this group. Try this one...

microsoft.public.access.macros

Other ways to evaluate a dynamic expression inside the code??

I don't program Office macros myself, so I don't know if VBA macros
expose a Controls collection or not, but this code will work in VB6...

Dim X As Long
For X = 1 To 3
Me.Controls("Text" & CStr(X)).Text = "TextBox #" & CStr(X)
Next

Me is a reference to the Form object the controls are on, so you might
have to substitute the VBA macro way of referring to the parent form.

Rick
Aug 21 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.