+ Reply to Thread
Results 1 to 11 of 11

Class module for userform textboxes

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2020
    Location
    Belgium
    MS-Off Ver
    2016
    Posts
    5

    Class module for userform textboxes

    I have
    Private Sub Textbox_01_Change()
        Textbox_01.Value = Val(Textbox_01) * Val(Textboxb_01)
    End Sub
    Private Sub Textbox_02_Change()
        Textbox_02.Value = Val(Textbox_02) * Val(Textboxb_02)
    End Sub
    and so on for 40 textboxes
    What would be the class module for 40 textboxes.

  2. #2
    Valued Forum Contributor dotchiejack's Avatar
    Join Date
    05-21-2015
    Location
    Antwerp,Belgium
    MS-Off Ver
    2016
    Posts
    507

    Re: Class module for userform textboxes

    Sorry, I was wrong,misunderstand the question.
    Last edited by dotchiejack; 06-02-2020 at 02:48 AM.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Class module for userform textboxes

    You need to post example file as your current code is not clear on how it is used.
    On the surface it looks like you are multiplying the contents of a textbox by the contents of another textbox. But as you are doing this in the change event everytime you update the textbox it will trigger the change event again and carry on in a loop.

    Your class would need to include 2 textbox objects.
    Cheers
    Andy
    www.andypope.info

  4. #4
    Registered User
    Join Date
    06-01-2020
    Location
    Belgium
    MS-Off Ver
    2016
    Posts
    5

    Re: Class module for userform textboxes

    Hi Andy,
    Thanks for responding.
    I made a small example, but I have a whole list like this.
    Please advice.
    Attached Files Attached Files

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Class module for userform textboxes

    Userform module
    Private mInputs As Collection
    Private Sub UserForm_Initialize()
    
        Dim index As Long
        Dim inputCtl As Class1
        
        Set mInputs = New Collection
        For index = 1 To 3
            Set inputCtl = New Class1
            Set inputCtl.Price = Me.Controls("TextBox" & index & "p")
            Set inputCtl.Number = Me.Controls("TextBox" & index & "n")
            Set inputCtl.Total = Me.Controls("TextBox" & index & "t")
    
            mInputs.Add inputCtl, CStr(mInputs.Count + 1)
        Next
        
    End Sub
    
    
    Private Sub UserForm_Terminate()
    
        Do While mInputs.Count > 0
            mInputs.Remove mInputs.Count
        Loop
        Set mInputs = Nothing
        
    End Sub
    Class module, Class1
    Option Explicit
    
    Public WithEvents Number As MSForms.TextBox
    Public Price As MSForms.TextBox
    Public Total As MSForms.TextBox
    
    Private Sub Class_Terminate()
    
        Set Number = Nothing
        Set Total = Nothing
        Set Price = Nothing
        
    End Sub
    
    
    Private Sub Number_Change()
        Dim result As Variant
        
        result = "0"
        On Error Resume Next
        
        result = Val(Price.Text) * Val(Number.Text)
        Total.Text = result
        
        Exit Sub
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    06-01-2020
    Location
    Belgium
    MS-Off Ver
    2016
    Posts
    5

    Re: Class module for userform textboxes

    Thank you Andy,great job.

  7. #7
    Registered User
    Join Date
    06-01-2020
    Location
    Belgium
    MS-Off Ver
    2016
    Posts
    5

    Re: Class module for userform textboxes

    Hi, I have a next question.
    What would be te cllas module solution if I want the total of the 3 TextBoxt textboxes.
    Thanks to look into my problem.
    Attached Files Attached Files
    Last edited by Knoet; 06-03-2020 at 12:18 PM.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Class module for userform textboxes

    You could have added a public sub to the userform and used that.
    But I have used another class object to centralise the callbacks to the userform.


    Class module, CEventListener

    Option Explicit
    
    Public Event TotalChange()
    Public Sub Total_Change()
        RaiseEvent TotalChange
    End Sub
    revised Class1 module code
    Option Explicit
    Public WithEvents Number As MSForms.TextBox
    Public Price As MSForms.TextBox
    Public total As MSForms.TextBox
    Public WithEvents Listener As CEventListener
    
    Private Sub Class_Terminate()
        Set Number = Nothing
        Set total = Nothing
        Set Price = Nothing
        Set Listener = Nothing
    End Sub
    
    Private Sub Number_Change()
        Dim result As Variant
        result = "0"
        On Error Resume Next
        result = Val(Price.Text) * Val(Number.Text)
        total.Text = result
        
        Me.Listener.Total_Change
        
        Exit Sub
    End Sub
    revised userform code
    Private mInputs As Collection
    Private WithEvents mEventListener As CEventListener
    
    Private Sub mEventListener_TotalChange()
    
        Dim index As Long
        Dim total As Double
        
        For index = 1 To 3
            total = total + Val(Me.Controls("TextBox" & index & "t").Text)
        Next
        Me.TextBox4.Text = total
        
    End Sub
    Private Sub UserForm_Initialize()
    Dim index As Long
    Dim inputCtl As Class1
    
        Set mEventListener = New CEventListener
        
        Set mInputs = New Collection
        For index = 1 To 3
            Set inputCtl = New Class1
            Set inputCtl.Price = Me.Controls("TextBox" & index & "p")
            Set inputCtl.Number = Me.Controls("TextBox" & index & "n")
            Set inputCtl.total = Me.Controls("TextBox" & index & "t")
            Set inputCtl.Listener = mEventListener
        mInputs.Add inputCtl, CStr(mInputs.Count + 1)
        Next
    End Sub
    Private Sub UserForm_Terminate()
        Do While mInputs.Count > 0
            mInputs.Remove mInputs.Count
        Loop
        Set mInputs = Nothing
        Set mEventListener = nothing
    End Sub
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    06-01-2020
    Location
    Belgium
    MS-Off Ver
    2016
    Posts
    5

    Re: Class module for userform textboxes

    Hi Andy,
    Thank you for your reply.
    I have a problem as we use comma's instead of dots, the calculatins are not correct. I tryed to replace the comma's by dots but i still have wrong calculations.
    Any ideas?
    Thanks

  10. #10
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,301

    Re: Class module for userform textboxes

    Quote Originally Posted by Knoet View Post
    ... I have a problem as we use comma's instead of dots, the calculatins are not correct. I tryed to replace the comma's by dots but i still have wrong calculations ...
    How did you try to change it ?

    Just use the 'Replace' function to achieve the desired effect, e.g.:
        Dim nmbrtxt, prctxt, rslt
        
        If Not IsNumeric(Number.Value) Or Not IsNumeric(Price.Value) Then Exit Sub
        
        nmbrtxt = Replace(Number.Value, ",", ".")
        prctxt = Replace(Price.Value, ",", ".")
        rslt = Replace(Val(prctxt) * Val(nmbrtxt), ".", ",")
        TotalSum.Value = rslt

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Class module for userform textboxes

    If the issue is with numeric separators and decimal delimiter, then it would not have been working for you at the start.

    I assume for you the number ten you would use comma for decimal 10,0 rather than the period I would expect with UK settings 10.0

    What does the VAL function return for the test values? You will need to step through the code and check or use debug.print commands to write to the immediate window

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Custom Class Module for multiple UserForm controls?
    By AnthonyGFS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-25-2018, 03:36 AM
  2. Place code in userform, module or class module
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2017, 07:04 AM
  3. Add Property to UserForm via class module - please help
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-07-2016, 09:35 AM
  4. [SOLVED] Userform ComboBox Class Module not working
    By rodgersmg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-24-2016, 03:23 AM
  5. [SOLVED] Userform & Dynamic Controls & Class Module
    By sarndt01 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 09-18-2014, 01:53 PM
  6. Compare even and odd numbered TextBoxes in a class module
    By KratjeBier2 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-03-2014, 03:39 PM
  7. Replies: 1
    Last Post: 10-12-2012, 04:36 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1