PDA

View Full Version : [SOLVED] Class Modules - a few questions



zagrijs
05-29-2014, 02:32 AM
:hi:



I noticed in the code editor that "class" is listed as a object of the class module and has two events, i.e. initialize and terminate. I could not find any information on how to use it. Could anybody please explain to me what it is for and how to use it?
If a custom object, e.g. textbox is made a custom object with a "public withevents" in a class module, is possible to create custom properties and events and attach them to the custom textbox?


Any help appreciated

Zagrijs

Bob Phillips
05-29-2014, 03:55 AM
1. Huge question. And it is highly debatable whether, application events apart, class modules are actually necessary in VBA. Personally I use them loads, but I can't even convince my brother of the efficacy of them.

2. No, I don't think so. But you can control a whole number of form controls from within the class code, rather than repeat the code with the actual controls event code. One of my favourite uses is to create a form event manager, and specify the data type for a given textbox, and control what is entered from within that event class. Unfortunately, a control has two levels of events, the control's events, and its container events. The events that are supported by the container, work fine in the userform, but these are not exposed to a form event class.

mikerickson
05-29-2014, 04:03 PM
In re:2,

The user can attach custom events to custom objects. One could write code for a custom textbox that fired an event when an "D" is entered into the text box. BUT... how to detect it?

At the base of a custom object with custom events are the native Excel objects and events.


'in userform code module

Public WithEvents CustomTextBox As clsCustomTextBox

Private Sub CustomTextBox_DEntered()
MsgBox "D was pressed"
End Sub

Private Sub UserForm_Initialize()
Set CustomTextBox = New clsCustomTextBox
Set CustomTextBox.myBox = TextBox1
End Sub


'in clsCustomTextBox class module

Public WithEvents myBox As MSForms.TextBox
Event DEntered()

Property Get HasD()
HasD = myBox.Text Like "*D"
End Property

Private Sub myBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not (Me.HasD) And Chr(KeyAscii) = "D" Then RaiseEvent DEntered
End Sub

Paul_Hossler
05-29-2014, 05:51 PM
If I understood the question ...

The .Initialize fires when you Set your object variable to a New instance of the Class, and .Terminate fires when you Set your class variable = Nothing

Simple made-up example of a standard module and a class module



Option Explicit
Sub demo()
Dim MyDate As clsDate, YourDate As clsDate

'fires the .Initialize
Set MyDate = New clsDate
Set YourDate = New clsDate

MsgBox MyDate.TheDate
MsgBox MyDate.Tomorrow

Call YourDate.AdjustDate(7)
MsgBox YourDate.TheDate

'fires the .Terminate
Set YourDate = Nothing
Set MyDate = Nothing

End Sub






Option Explicit
Private dtToday As Date
Private Sub Class_Initialize()
dtToday = Date
MsgBox "Init to " & Date
End Sub
Private Sub Class_Terminate()
MsgBox "Done with the date = " & dtToday
End Sub
Property Get TheDate() As Date
TheDate = dtToday
End Property
Property Let TheDate(D As Date)
dtToday = D
End Property
Sub AdjustDate(iDays As Long)
dtToday = dtToday + iDays
End Sub
Property Get Tomorrow() As Date
Tomorrow = dtToday + 1
End Property



So any init or setup can be done when the object is instantiated, and any cleanup can be done when you clear the object

zagrijs
05-30-2014, 12:23 AM
Thanks to all replies.

I'm a self trained developer and still on a steep learning curve. It will take some time to "digest" the the info and examples by mikerickson and Paul_Hausler. Therefore I'm not going to mark my post "solved" yet.

With reference to xld's remarks. I also use class modules a lot and in the way you describe. Circumstances have forced me to use VBA, but I would have preferred to rather use dBASE as front end. dBASE allows you to create custom forms and custom controls. Every form based on the custom form automatically inherits every control, coded event, function, etc of the custom form. When you open any form in the code editor, the code for the form and controls are also opened with the events and functions and can be modified. If the developer wants to change a textbox to combobox, he doesn't have to replace the control in the form designer, but he can simply change the class name of the control in the code editor. One can create any number of custom controls with any number of custom properties, all in one module, and custom controls based on custom controls - ad infinitum! Every descendant custom control inherits all the properties, coded events, functions, etc of all the custom controls in its up-line. The custom controls are available on the palette and you simply drag them to the form.

Bob Phillips
05-30-2014, 12:58 AM
Mike's comment is especially helpful I think, I forgot about that technique when I replied, so take a good look at that, it opens up a number of possibilities. But you are so right, VBA is a long way from OOP and you have to do everything yourself. I personally detest userforms in VB/VBA, they are such hard work. Where I can, I would much prefer to dump the data onto a worksheet and manipulate that, Excel offers far richer functionality