Consulting

Results 1 to 4 of 4

Thread: Rename controls in VBE

  1. #1
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location

    Rename controls in VBE

    Is there a way to interate the controls in the VBE, renaming the object name and the caption?

    I tried this code.
    [VBA]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 Sub[/VBA]But 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!

    David


  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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.

  3. #3
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    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.

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

    David


  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •