?
Solved

Events not firing at client

Posted on 2003-02-20
4
Medium Priority
?
226 Views
Last Modified: 2010-05-01
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.Show
    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.Hide
    frmSelectDate.Visible = False
    clsDate.RFormClosed
End Sub
*end form code
*end ActiveX exe code

**Client Code
Private WithEvents Datx As DateSelector

Private Sub Command1_Click()
    Datx.OpenForm
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
    Datx.Initialization
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Datx.Terminatation
    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.
0
Comment
Question by:hibbt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
inthedark earned 800 total points
ID: 7988888
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

To

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.....

or

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")
   Caller.RDateSelected
   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:~)
0
 
LVL 27

Expert Comment

by:Dabas
ID: 7988969
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!

Dabas
0
 

Author Comment

by:hibbt
ID: 7990130
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
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7990549
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!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question