PDA

View Full Version : Rename controls in VBE



Tinbendr
10-08-2009, 08:33 AM
Is there a way to interate the controls in the VBE, renaming the object name and the caption?

I tried this code.
Private Sub setup()
Dim aControl As Control
Dim Counter As Integer
For Each aControl In ufBenchStockPick.Controls
If aControl.Name Like "CommandButton*" Then
Counter = Counter + 1
'Rename control
aControl.Name = "cmdBS" & Right("00" + Trim(Counter), 3)
'Rename Caption of Control
aControl.Caption = Right("00" + Trim(Counter), 3)
End If
Next
End SubBut it stops at aControl.Name with "cannot change at runtime" error.

I can take out the name method and just let it rename the caption, but it doesn't change it on the VBE, just at Userfrom1.Show.

Thanks in advance!

JP2112
10-08-2009, 06:42 PM
It makes sense that you can't change the Name property at runtime, since the name of the control can drive other things like event handlers, which depend on the name of the control. I don't think there's anything you can do about it.

As far as the caption, there's a difference between design time and runtime. To change something in the IDE at design time, you have to do it while physically editing the userform, viewing the Properties window, moving controls around on the design surface, etc. What you're doing is changing the caption at runtime -- that won't affect the design-time caption. It's like loading a picture in an Image control at runtime: you won't see the picture in the IDE, but when your form loads, the image appears.

I always leave my userforms with the caption "UserForm1" and set the caption at runtime, so I don't have to edit the caption property every time I feel like changing the form name.

Tinbendr
10-08-2009, 07:27 PM
OK thanks!

The caption isn't the problem, it's the object name. I've been asked to design a multipage commandbutton (500+) userform and I was trying to rename the objects without having the manually do it. :p

I thought I might be able to do this through the VBAProject, but I haven't found anything elsewhere either.

JP2112
10-09-2009, 04:21 AM
Just remember that when you rename controls, any event handlers associated with that event will not change.

So if you change a TextBox Control named 'ZipCode' to 'CustomerZipCode', the Change event won't be renamed. You'll have to manually change ZipCode_Enter to CustomerZipCode_Enter. In some cases, you'll need to recreate the event entirely.