PDA

View Full Version : Solved: Calling Sub at ClassModule for UserForm



omnibuster
12-16-2012, 02:27 PM
Hellou.
I am stack this problem!
Is it possible to call variable (CommandButton.Name or Caption) for UserForm.

snb
12-16-2012, 03:06 PM
Keep it simple

omnibuster
12-17-2012, 09:54 AM
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:
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


In UserForm1 module:

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

omnibuster
01-02-2013, 03:06 PM
Can anyone help with this?

GTO
01-02-2013, 11:38 PM
Hi 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:

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

In a Class named: clsButton

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

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

Hope that helps,

Mark

omnibuster
01-03-2013, 08:57 AM
Big thanks GTO.

Happy New Year!

snb
01-04-2013, 04:10 AM
there's a much simpler way:

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


Private Sub btn_Click()
btn.parent.Nupud btn.Caption
End Sub

omnibuster
01-04-2013, 02:25 PM
Thanks snb.

Now i see in

__keep it simple snb.xlsm (http://www.vbaexpress.com/forum/attachment.php?attachmentid=9258&d=1355695604)


Public Sub v_button_Click()
v_button.Parent.Caption = v_button.Name
End Sub


Sry I´m such blind. :)