Label caption doesn't display on userform after update using Class modules

  • Hello,


    I have a userform that will have multiple text boxes for users to edit data pulled from one of the Sheets.
    In that userform I have corresponding labels for each text box to show the number of characters each has.
    I want each respective label to update their caption to be displayed as the user is editing each text box.


    The method I have come across to do this is by using a Class module.
    However, I am having an issue with the label caption not displaying on the userform.
    The below code is an example of the code I'm using on just a single text box and single corresponding label named ItemNumber and ItemNumberLength respectively:


    Class Module:



    Userform1:



    Thanks in advance!

  • Re: Label caption doesn't display on userform after update using Class modules


    .
    If I correctly understand your question, you can simplify your efforts with :


    Code
    Option Explicit
    
    
    Private Sub TextBox1_Change()
        Label1.Caption = TextBox1.Text
    End Sub
  • Re: Label caption doesn't display on userform after update using Class modules


    Hi Logit. Thanks for the reply.
    Unfortunately that only works if I write that code for every textbox I would have in the userform. So if I had say 20 textboxes, I would have to include a sub for each of them. Creating the Class module eases that as it let's me use just a few lines of code to step thru all of the textboxes at once. It seems to work as when I include a MsgBox, that pops up every time I make a change in the textboxes. So, I know that it calls the sub PersistentUpdate_ItemNumber()...there just seems to be a problem with it displaying the caption after it's changed.

  • Re: Label caption doesn't display on userform after update using Class modules


    Hi '
    Possibly with your code
    Class module named clsTextBox

    Code
    Option Explicit
    Private WithEvents MyTextBox As MSForms.TextBox
     
    Public Property Set Control(tb As MSForms.TextBox)
    Set MyTextBox = tb
    End Property
     
    Private Sub MyTextBox_Change()
        Call UserForm1.PersistentUpdate_ItemNumber(MyTextBox.Name)
    End Sub


    and in the userform1 module

  • Re: Label caption doesn't display on userform after update using Class modules


    Change the Sub to:


    Code
    Sub PersistentUpdate_ItemNumber(MyName As String)
        Dim myIndex As Long
        myIndex = Right(MyName, Len(MyName) - 7)
        Me("Label" & myIndex).Caption = Me("Textbox" & myIndex).Text
    End Sub


    Thanks Pike !

  • Re: Label caption doesn't display on userform after update using Class modules


    Hmm...I seem to be having an issue passing in the MyTextBox.Name
    It keeps giving me a Type Mismatch error?

  • Re: Label caption doesn't display on userform after update using Class modules


    .
    At first, had same issue. My solution was overlooked in his post.

    Quote

    Class module named clsTextBox


    Rename the Class Module to ' clsTextBox ' . I had to try that several times for some reason, until it finally took hold.

  • Re: Label caption doesn't display on userform after update using Class modules


    Thanks Logit and Pike for your input!
    I finally got it to work by just starting from scratch again.


    However, when I tried to reuse it in the workbook I've been working on it went right back to the same problem I was previously having. That is that the Label caption wasn't displaying on the UserForm.
    So, I apparently was having two separate issues...


    For the sake of other readers, so that they don't make the same mistake I did.
    I found the second issue to be in how I was calling the userform. I originally had a button on the worksheet that called a macro to run various other tasks before it called the userform.


    I don't recall why I originally wrote it this way, but the example below shows how I was calling it...as opposed to the much simpler way that actually works properly:


    (Doesn't work)

    Code
    Sub RunUserFrom()        
    	Dim myUserform As UserForm1
            Set myUserform = New UserForm1
            myUserform.Show
    End Sub


    (Does Work)

    Code
    Sub RunUserForm()        
    	UserForm1.Show
    End Sub


    Thanks again both for helping me figure this out! I've been banging my head against the wall over this for days now...Haha.

  • Re: Label caption doesn't display on userform after update using Class modules


    You're welcome. Glad to help. I learned alot from your post as well.

  • Re: Label caption doesn't display on userform after update using Class modules


    FWIW, I would suggest you add a label variable to the class and simply refer to that:



    and then something like this:

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: Label caption doesn't display on userform after update using Class modules


    Cool


    class code



    enhancement to userform code


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!