Looping thru Textboxes in Userform 
Author Message
 Looping thru Textboxes in Userform

In my Word document, I have a bookmark to go to the first cell in a table. I
have 15 textboxes on my userform named Textbox2 thru Textbox16. I want to
pick each one in a loop and lay down the text of each in succession.

So far, I have:

ActiveDocument.Bookmarks("Attends").Select
For X = 2 to 16
    Selection.Text = TextBox(X)
    Selection.MoveRight Unit:=wdCell
Next

The TextBox(X) does not work; what is my alternative.

Alan



Wed, 27 Apr 2005 11:14:45 GMT  
 Looping thru Textboxes in Userform

Quote:
> ActiveDocument.Bookmarks("Attends").Select
> For X = 2 to 16
>     Selection.Text = TextBox(X)
>     Selection.MoveRight Unit:=wdCell
> Next

Try "Next X" at end


Wed, 27 Apr 2005 19:15:15 GMT  
 Looping thru Textboxes in Userform
Hi Alan,

there are several ways to do this, but the way you have chosen is not one of
them. In brief, TextBox7 (for instance) is not the same as TextBox(7). The
first is a stand-alone variable name, the second is the seventh instance of
a different name.

Also, you haven't specified the .Text property in the TextBox. I don't
remember now if that's the default property, but even if it is, it's good
practice to specify it anyway.

There are two ways to do what you want, some of it depends on what else is
on the UserForm.

The simplest is this:

Dim ctl As Control
For Each ctl In UserForm1
    Selection.Text = ctl.Text
    Selection.MoveRight unit:=wdCell
Next ctl

but this will work only if there are NO other controls on the UserForm
besides your TextBoxes, and if the TextBoxes are on the UserForm in
numerical order. If the order is correct, but there are other controls mixed
in, you can overcome that by modifying the code this way:

Dim ctl As Control
For Each ctl In UserForm1
    If ctl.Type = TextBox Then
        Selection.Text = ctl.Text
        Selection.MoveRight unit:=wdCell
    End If
Next ctl

or, if you specifically want to refer only to the TextBoxes and strictly by
name, you can do this:

Dim x&
For x = 2 To 16
    Selection.Text = UserForm1.Controls("TextBox" & x).Text
    Selection.MoveRight Unit:=wdCell
Next x

assuming that your UserForm is named UserForm1. You see in the third line,
the name must be assembled as a string and passed to the Controls collection
for retrieval. Code text and string literal text are not interchangeable or
mixable - they must each be handled a different way.

--
Pete

This e-mail address is fake to keep spammers and their auto-harvesters out
of my hair. If you need to get in touch personally, I am 'pdanes' and I use
Yahoo mail. But please use the newsgroups whenever possible, so that all may
benefit from the exchange of ideas.

----------------------------------------------------------------------------
----


Quote:
> In my Word document, I have a bookmark to go to the first cell in a table.
I
> have 15 textboxes on my userform named Textbox2 thru Textbox16. I want to
> pick each one in a loop and lay down the text of each in succession.

> So far, I have:

> ActiveDocument.Bookmarks("Attends").Select
> For X = 2 to 16
>     Selection.Text = TextBox(X)
>     Selection.MoveRight Unit:=wdCell
> Next

> The TextBox(X) does not work; what is my alternative.

> Alan



Thu, 28 Apr 2005 02:42:54 GMT  
 Looping thru Textboxes in Userform
Thanks, that looks like what I need.


Quote:
> Hi Alan,

> there are several ways to do this, but the way you have chosen is not one
of
> them. In brief, TextBox7 (for instance) is not the same as TextBox(7). The
> first is a stand-alone variable name, the second is the seventh instance
of
> a different name.

> Also, you haven't specified the .Text property in the TextBox. I don't
> remember now if that's the default property, but even if it is, it's good
> practice to specify it anyway.

> There are two ways to do what you want, some of it depends on what else is
> on the UserForm.

> The simplest is this:

> Dim ctl As Control
> For Each ctl In UserForm1
>     Selection.Text = ctl.Text
>     Selection.MoveRight unit:=wdCell
> Next ctl

> but this will work only if there are NO other controls on the UserForm
> besides your TextBoxes, and if the TextBoxes are on the UserForm in
> numerical order. If the order is correct, but there are other controls
mixed
> in, you can overcome that by modifying the code this way:

> Dim ctl As Control
> For Each ctl In UserForm1
>     If ctl.Type = TextBox Then
>         Selection.Text = ctl.Text
>         Selection.MoveRight unit:=wdCell
>     End If
> Next ctl

> or, if you specifically want to refer only to the TextBoxes and strictly
by
> name, you can do this:

> Dim x&
> For x = 2 To 16
>     Selection.Text = UserForm1.Controls("TextBox" & x).Text
>     Selection.MoveRight Unit:=wdCell
> Next x

> assuming that your UserForm is named UserForm1. You see in the third line,
> the name must be assembled as a string and passed to the Controls
collection
> for retrieval. Code text and string literal text are not interchangeable
or
> mixable - they must each be handled a different way.

> --
> Pete

> This e-mail address is fake to keep spammers and their auto-harvesters out
> of my hair. If you need to get in touch personally, I am 'pdanes' and I
use
> Yahoo mail. But please use the newsgroups whenever possible, so that all
may
> benefit from the exchange of ideas.

> --------------------------------------------------------------------------
--
> ----



> > In my Word document, I have a bookmark to go to the first cell in a
table.
> I
> > have 15 textboxes on my userform named Textbox2 thru Textbox16. I want
to
> > pick each one in a loop and lay down the text of each in succession.

> > So far, I have:

> > ActiveDocument.Bookmarks("Attends").Select
> > For X = 2 to 16
> >     Selection.Text = TextBox(X)
> >     Selection.MoveRight Unit:=wdCell
> > Next

> > The TextBox(X) does not work; what is my alternative.

> > Alan



Thu, 28 Apr 2005 04:33:07 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Loop thru every textbox in a table

2. Userform: populate textbox with date from another textbox

3. Enter text into document thru userform

4. Looping thru records

5. Huge Slowdown While Looping Thru FileFind Procedure

6. Looping Thru a Tables Records

7. How to loop thru all appointments at a specific time

8. Looping thru the items

9. looping thru characters in a string

10. Selection.Find.Execute does not find the text 2nd time thru the loop

11. Loop Thru all Controls in a Document

12. Looping Thru Files in a Folder

 

 
Powered by phpBB® Forum Software