[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Events not firing at client

When an event is raised outside of class module code (via Friend sub) with in the same ActiveX exe, the client application is not seeing the event.

**ActiveX exe code
*class module DateSelector
Public Event UserCancelled()
Public Event DateSelected()
Public Event FormOpened()
Public Event FormClosed()
Public DatePicked As Date

Private Sub Class_Terminate()
    Set frmSelectDate = Nothing
End Sub
Public Sub Initialization()
    Load frmSelectDate
End Sub
Public Sub Terminatation()
    Call TerminateObjects
End Sub
Public Sub OpenForm()
    frmSelectDate.Visible = True
    Call RFormOpened
End Sub
Friend Sub RUserCancelled()
    RaiseEvent UserCancelled
End Sub
Friend Sub RDateSelected()
    RaiseEvent DateSelected
End Sub
Friend Sub RFormOpened()
    RaiseEvent FormOpened
End Sub
Friend Sub RFormClosed()
    RaiseEvent FormClosed
End Sub

*end class code
*.bas code
Public clsDate As DateSelector

Sub Main()
    Set clsDate = New DateSelector
End Sub

Public Sub TerminateObjects()
    Set clsDate = Nothing
End Sub
*end .bas code
*form code frmSelectDate
Private Sub calSelectDate_Click()
    clsDate.DatePicked = Format(calSelectDate.Value, "mm/dd/yy")
    Call clsDate.RDateSelected
    Call CloseForm
End Sub

Private Sub CloseForm()
    frmSelectDate.Visible = False
End Sub
*end form code
*end ActiveX exe code

**Client Code
Private WithEvents Datx As DateSelector

Private Sub Command1_Click()
End Sub

Private Sub Datx_DateSelected()
    MsgBox Datx.DatePicked
End Sub

Private Sub Datx_FormOpened()
    MsgBox "Form Opened"
End Sub

Private Sub Form_Load()
    Set Datx = New DateSelector
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set Datx = Nothing
End Sub
*end client code

Interestingly enough, the Private Sub Datx_FormOpened() event will fire, but the Private Sub Datx_DateSelected() will not fire.  I have stepped throught the application and the RaiseEvent code is being executed, it is just not firing on the client.
  • 2
1 Solution
You are alomost there but you are confusing your objects.....so it should be simple to fix.

The problem is that you are declaring an object in your BAS module like this:

Public clsDate As DateSelector

If you used the WithEvents keyword you would see that the DateSelected event if firing in the BAS module.  In fact you should not decalre the object in you bas module at all.

So to fix the problem here are the steps you need to take:

1) In the class module

Change this

Public Sub Initialization()
   Load frmSelectDate
End Sub


Dim MyForm As Form
Public Sub Initialization()
  Set MyForm = New frmSelectDate
  ' tell the form which object he is associated with
  Set MyForm.Caller = Me
End Sub

2) And also in the class change terminate as follows:

Private Sub Class_Terminate()
   Unload MyForm
   Set MyForm = Nothing
End Sub

3) Change all other references of frmSelectDate to MyForm.

4) In the form frmSelectDate you need

Public Caller as DateSelector

You can now send messages back to client instance of the class by using Caller

Caller.RaiseEvent etc.....


Caller.AnyMethod AnyStuff

Unless you are very carefull BAS modules in an activex exe will cause you problems.

Example in your frmSelectDate you could do it like this:

Private Sub calSelectDate_Click()
   Caller.DatePicked = Format(calSelectDate.Value, "mm/dd/yy")
   me.hide ' or any other form cleanup but....
'  Call CloseForm don't need to close form as
' class terminate will do that when appropriate
End Sub

Hope this helps:~)
What is happening here is that you have a client form, that references the DateSelector Class.

The class then opens another form (frmSelectDate) which presents a date to pick.

When I tried to run your code, the following line produced a compile error:

clsDate.DatePicked = Format(calSelectDate.Value, "mm/dd/yy")

I assume you have more code that you posted in your application, but it does give us a clue as to what is going wrong.
Probably clsDate in frmSelectDate does not reference the same instance of the class as the original form did.
Therefore, although it is raising the event, the event is not handled anywhere!

To solve the problem, declare clsDate in frmSelectDate as Public and add the following line after frmSelectDate.Show in your class:

   Set frmSelectDate.clsDate = Me

This way clsDate points to the same object as Datx and the event fires correctly!

hibbtAuthor Commented:
Thanks inthedark.  I am accepting your answer.  I have never used:
Set MyForm.Caller = Me
Do you have a link to further documentation?

Thanks again
Caller is just a variable name.  When you craete an instance to an object, if that object creates instances of other objects you need a system for passing info back to the caller object.

Sorry, it is not easy to understand!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now