Consulting

Results 1 to 8 of 8

Thread: Solved: Calling Sub at ClassModule for UserForm

  1. #1

    Solved: Calling Sub at ClassModule for UserForm

    Hellou.
    I am stack this problem!
    Is it possible to call variable (CommandButton.Name or Caption) for UserForm.
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Keep it simple
    Attached Files Attached Files

  3. #3
    Thanks snb.
    This too but biggest problem with Call Nupud command?
    Is it possible call Sub Nupud in Class1 module and get answer (Button i.e cmb1.caption in UserForm1 module - in red).

    Like this:

    In Class module:
    [vba]Public Button As String
    Private WithEvents cmb1 As MSForms.CommandButton

    Sub SetCommandButton(ByVal cmb As MSForms.CommandButton)
    Set cmb1 = cmb
    End Sub

    Public Sub cmb1_Click()
    MsgBox cmb1.Name
    Button = cmb1.Name

    '#Error: Sub or function not defined
    Call Nupud

    End Sub[/vba]


    In UserForm1 module:

    [vba]Private WithEvents cmb1 As MSForms.CommandButton
    Dim cmb() As Class1

    Public Sub Nupud(Button As String)
    Dim i As Long
    Dim b As Long
    Dim a As Integer

    MsgBox Button 'cmb1.Name

    With ListBox1
    If ListBox1.ListCount = 0 Then
    MsgBox "There is nothing in the list to print!"
    Exit Sub
    Else
    b = ListBox1.ListCount
    End If


    Do Until a = ListBox1.ListCount

    If ListBox1.Selected(a) = True Then


    Select Case Button 'cmb1.Name

    Case "Delete" 'cmb1.Name
    ListBox1.RemoveItem a: a = a - 1


    Case "Moove" 'cmb1.Name
    Me.ListBox2.AddItem ListBox1.List(a)
    ListBox2.Locked = True
    ListBox1.RemoveItem a: a = a - 1



    Case "Clear"
    'clear


    Case Else


    End Select


    Else

    a = a


    End If
    a = a + 1
    Loop
    If b = ListBox1.ListCount Then MsgBox "Make Choises"

    End With
    End Sub
    Private Sub UserForm_Activate()
    Dim AllCells As Range, Cell As Range
    Dim LastRow As Long
    LastRow = Range("A1").End(xlDown).Row
    Set AllCells = Range("A1:A" & LastRow)
    For Each Cell In AllCells
    UserForm1.ListBox1.AddItem CStr(Cell.Value)
    Next Cell
    ListBox1.MultiSelect = 1
    ListBox1.ListStyle = fmListStyleOption
    End Sub
    Private Sub UserForm_Initialize()

    Dim obj As MSForms.Control, i As Long
    For Each obj In Me.Controls
    If TypeName(obj) = "CommandButton" Then
    i = i + 1
    ReDim Preserve cmb(i)
    Set cmb(i) = New Class1
    cmb(i).SetCommandButton obj
    ' obj.Tag = i
    End If
    Next
    End Sub
    [/vba]

  4. #4
    Can anyone help with this?

  5. #5
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi omnibuster,

    Quote Originally Posted by omnibuster
    ...This too but biggest problem with Call Nupud command?
    Is it possible call Sub Nupud in Class1 module and get answer (Button i.e cmb1.caption in UserForm1 module - in red)...
    I believe I am understanding your question. You mean you want to call 'Nupud()', which resides in the form's module, and you want to call it from an instance of the class, right?

    To do this, the class needs to explicitly call the procedure, as the procedure itself, resides in a Class or Object module (in this case, an Object module). Think of this like calling a Public procedure residing in a Worksheet's module. It can be done, but the call has to be explicit. Like 'Sheet1.MyFunctionInAWorksheet()' instead of just 'MyFunctionInAWorksheet()' {see the example MAIN() in the Standard module}.

    Anyways, here is a short example:

    In UserForm: 'frmMyUserform'
    Three CommandButtons named: cmdButton1of3, cmdButton2of3, cmdButton3of3

    In the UserForm's Module:

    [vba]Option Explicit

    Private btns(1 To 3) As clsButton

    Private Sub UserForm_Initialize()
    Dim n As Long

    ' just for clarity of example
    With Me
    .cmdButton1of3.Caption = "One"
    .cmdButton2of3.Caption = "Two"
    .cmdButton3of3.Caption = "Three"
    End With

    ' However we loop thru the controls, set a reference to a new instance
    ' of the Class, AND, ensure the class knows what Form it needs to access.
    For n = 1 To 3
    Set btns(n) = New clsButton
    Set btns(n).ReferenceForm = Me
    Set btns(n).ReferenceButton = Me.Controls("cmdButton" & n & "of3")
    Next
    End Sub

    Public Sub Nupud(Caption As String)

    Select Case Caption
    Case "One"
    Me.Caption = "cmdButton1of3 was pressed"
    Case "Two"
    Me.Caption = "cmdButton2of3 was pressed"
    Case "Three"
    Me.Caption = "cmdButton3of3 was pressed"
    End Select
    End Sub[/vba]

    In a Class named: clsButton

    [vba]Option Explicit

    Private frm As Object
    Private WithEvents btn As MSForms.CommandButton

    Property Set ReferenceForm(form As Object)
    Set frm = form
    End Property
    Property Get ReferenceForm() As Object
    Set ReferenceForm = frm
    End Property

    Property Set ReferenceButton(button As MSForms.CommandButton)
    Set btn = button
    End Property
    Property Get ReferenceButton() As MSForms.CommandButton
    Set ReferenceButton = btn
    End Property

    Private Sub btn_Click()
    ReferenceForm.Nupud ReferenceButton.Caption
    End Sub[/vba]

    As you see, we now properly qualify the call to Nupud().

    Hope that helps,

    Mark
    Attached Files Attached Files

  6. #6
    Big thanks GTO.

    Happy New Year!

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    there's a much simpler way:

    the only code you need to perform this in the class module:

    [VBA]
    Private Sub btn_Click()
    btn.parent.Nupud btn.Caption
    End Sub
    [/VBA]

  8. #8
    Thanks snb.

    Now i see in
    [vba]
    Public Sub v_button_Click()
    v_button.Parent.Caption = v_button.Name
    End Sub
    [/vba]

    Sry I´m such blind.

Posting Permissions

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