Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Imelda Miller   on Nov 25 In MS Office Category.

  
Question Answered By: Abasi Massri   on Nov 25

I tried setting up a routine to change  the names  only to discover you
can't change controls' names at runtime - but you can if you've added
the control at runtime.

So there's a work around you might like, as any label/textbox changes
you make on the first page  of the multipage  will be reflected in the
other pages automatically.

I set up a userform  (Userform1) with a multipage (MultiPage1) and set
up a few text boxes and labels on the first page (Page(0)), added a
further two pages to it to make 4 pages in total, and left the last
three pages empty.

The following code runs through each of the controls on the first page
and adds similar ones to the other three pages, same position and
size, but names them by stripping off the last character of the name
of the corresponding control on the first page and adding the single
digit of the page number it's on.

Sub blah()
For Each mctrl In UserForm1.MultiPage1.Pages(0).Controls
For i = 1 To 3
Set newCtrl = UserForm1.MultiPage1.Pages(i). _
Controls.Add("Forms." & TypeName(mctrl) & ".1")
With newCtrl
.Name = Left(mctrl.Name, Len(mctrl.Name) - 1) & i + 1
'remove the next two If statements, they're just to
'show the names of the controls on the form  itself, to debug.
If TypeName(newCtrl) = "Label" Then .Caption = .Name
If TypeName(newCtrl) = "TextBox" Then .Value = .Name
.Left = mctrl.Left
.Height = mctrl.Height
.Top = mctrl.Top
.Width = mctrl.Width
End With
Next i
Next mctrl
UserForm1.Show
End Sub

(This was in a standard module, but you could place it in the
Initialize event for the form itself with a little adjustment.)

There may be other controls on each page; if you need to filter out
only those you need renaming, come back to me.

Share: 

 
 


Tagged: