WithEvents Trouble

  • Thread starter Thread starter Deleted member 73419
  • Start date Start date
D

Deleted member 73419

Guest
I am having trouble writing a class that will pick up on form events. The idea being whenever a form event occurs, the class will pick it up and process it.

Currently I have a class with the following:
Code:
Option Compare Database
Public WithEvents dbr As Form
 
Private Sub Class_Initialize()
End Sub
 
Private Sub dbr_Load()
    MsgBox "Form loaded"
End Sub

Now somehow I need to link the forms to the event handlers which is where I am struggling.

Can anyone shed some light on how this could be achieved please? Any help is appreciated.

Thanks
 
The thing is you have to assign a form to the variable, then set up the handler if there are none.

Example:

Code:
Private Const Evented As String = "=[Event Prodcure]"

Public Sub Init(frm As Form)

Set dbr = frm

If dbr.OnLoad = "" Then
   dbr.OnLoad = Evented
End If

and call the Init as the first thing after you instantiate the class to assign the form you want to monitor.

HTH.
 
The thing is you have to assign a form to the variable, then set up the handler if there are none.

Example:

Code:
Private Const Evented As String = "=[Event Prodcure]"
 
Public Sub Init(frm As Form)
 
Set dbr = frm
 
If dbr.OnLoad = "" Then
   dbr.OnLoad = Evented
End If

and call the Init as the first thing after you instantiate the class to assign the form you want to monitor.

HTH.
Thanks Banana,

I now have this in the class module:
Code:
Option Compare Database
Public WithEvents dbr As Form
Private Const Evented As String = "=[Event Procedure]"
 
Private Sub Class_Initialize()
End Sub
Private Sub dbr_Load()
    MsgBox "Form loaded"
End Sub
 
Public Sub Init(frm As Form)
Set dbr = frm
If dbr.OnLoad = "" Then
   dbr.OnLoad = Evented
End If
End Sub
and this in the form:
Code:
Option Compare Database
Dim x As Class1
Private Sub Command0_Click()
    Set x = New Class1
    Call x.Init(Me)
 
End Sub

After setting a breakpoint, it does appear dbr is set to the form passed in to init but nothing happens when a form is loaded.

Am I missing something obvious? :o
 
No, it's probably me who goofed up as I was writing it off the top of head.

If you want to see an example of WithEvents, may I suggest you go and download this sample- it has the code that I long ago borrowed for my use...

http://www.peterssoftware.com/fv.htm

See if this helps correct my error?
 
You assign event properties such as just "[Event Procedure]"
(i.e. without the equal assignment).

The [Event Procedure] string makes Access raise the specifically named procedure in the class. The Equals pushes us into function mode. :-)

However in the case of this event - Load has long since been raised by the time that this class has been passed the form object.
You'll be OK with the other (subsequent) form events though.
Bear in mind you need to assign the [Event Procedure] (if it's not already assigned for the property) for every event in the form you want to sink. Otherwise it won't be raised in your class - because it's not raised by the form).

Cheers.
 
Leigh, thanks for correcting me. I seem to recall you having correcting me on this precise same mistake (putting in a equal sign in the event procedure. ) Such silly fellow I am.

As for Load being too late, I wonder if one could get away with using the form class instead? Not that I would want to do it as I've kind of found them to be just bit wonky:

Code:
Sub Class_Initalize()

Set dbr = Form_MyForm
dbr.Visible = True

End Sub

then the Load event should be exposed to the class.
 
For simplicity, I'd suggest just assigning the class object using the form in its Open event should be enough. (It's a command button click at present is all - so will be long after loading :-)

Of course - there's the blurry line that the Load event doesn't always completely follow the Open event... But that's not a likely consideration here.

I'm not convinced about the value of refering to the class object though.
If the form was already loaded then, well, we're back to where we were before, it's all already done.
If it wasn't - then refering to the class would create an implicit instance. Even if that's OK and we don't want a form created by other means... by the time the form reference is returned to the calling class object, it'll likely have already loaded still.

Cheers.
 
thanks for your help guys :)...I just about have my DB working as I want it!!
 

Users who are viewing this thread

Back
Top Bottom