Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a Class module

Status
Not open for further replies.

assiri

Technical User
Aug 17, 2008
17
0
0
GB
This is regarding the faq 707-4976,Understanding events (iv)A substitute for control arrays in VBA posted in this Forum.
The aim is to handle two command buttons named cmd1 and cmd2 in a user form named frmCol.
I put the following code in a class module named cCB and created a user form which has two command buttons cmd1 and cmd2(This code is from the faq 707-4976,I am not the original author)
could u please tell me what is the next step to handle two command buttons in the userform, using the created class module cCB.
-----------------------------------------------------------
Private WithEvents m_CB As MSForms.CommandButton
Private m_Form As frmCol

Public Sub Init(ctl As CommandButton, frm As frmCol)
Set m_CB = ctl
Set m_Form = frm
End Sub

Private Sub m_CB_Click()
m_Form.Info m_CB
End Sub

Private Sub Class_Terminate()
Set m_CB = Nothing
Set m_Form = Nothing
End Sub

An instance of this class should button click event (one instance– one button), and keep information on userform that instantiated it.
The frmCol module:

Private colCB As New Collection
Private ctlCB As cCB

Private Sub UserForm_Initialize()
Set ctlCB = New cCB
ctlCB.Init cmd1, Me
colCB.Add ctlCB
Set ctlCB = New cCB
ctlCB.Init cmd2, Me
colCB.Add ctlCB
End Sub

Public Sub Info(ctl As MSForms.CommandButton)
MsgBox "click by: " & ctl.Caption
End Sub
----------------------------------------------------------
Thankx
 
What do you mean by next step.? The Info procedure will fire when you click a button and the skeleton code in there simply tells you which button you have pressed; you must put whatever specific code you want in there for yourself.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Still I was not able get the required output(message box,displaying the command button caption)
please guide me if I have made any mistakes during the process.I did the following steps.
1)Opened a form in design view
2)Added two command buttons
3)changed commandbutton names to cmd1 and cmd2 in properties sheet.
4)saved the form as (m_Form)
5)Inserted a class module.
6)Copied and pasted the above code(complete code is given in my question, code in faq 707-4976)
7)saved the class module as cCB

could u please guide me if I have made any mistake or anything wrong
Thankx
 
Even though I change the file name to frmCol, I was unable to get the output.Anyway thankx Tony

assiri
 
I tried it as well and it works fine as long as you ensure your form and class are named correctly. I would just delete your old form and class and try it over from scratch.
 
I wonder why this is not working for me!!!
For the FINAL TRY please let me know any steps that I have missed in the process,
1)Opened a new form in design view
2)Added two command buttons
3)changed commandbutton names to (cmd1) and (cmd2) in properties sheet.
4)saved the form as (frmCol)
5)Inserted a class module.
6)Copied and pasted the above code(complete code is given in my question)
7)saved the class module as cCB
Thankx for the information,at least now I can clear my do doubts about this method because u have proven that it is working...
 
Looks right as far as I can tell.
1)Compile your code. Any issues?
2)Verify that you pasted the code in a Class module not a standard module.
 
Thankx...., There is a compile error in the codes' second line, which I have shown below.

Private m_Form As frmCol

The error message states "user defined data type not defined"

Tried to define "frmCol" data type using the {Type Endtype} statement but no good results.Please let me know any method to get rid of this error.

assiri
 
When you have created your userForm called frmCol, that will define the type.

You are putting your Class and your UserForm in the same Project, aren't you?

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top