I have a reference to the external application's dll.
How do I write an event handler in Access VBA to be fired when that external application generates an event?
To handle events you create a variable using the WithEvents keyword. This tells VBA to listen for events raised by that object. Syntax is like...
Code:
Private WithEvents m_so As SomeObjectModel.SomeObject
And then the event handler, the signature of which must match that of the event as sourced by the object, say ...
Code:
Private Sub m_so_SomeEvent(StringVariable As String)
'
End Sub
Also, VBA will write the event handler signature for you if you select the object in the dropdown at the top of the code window that says (General). The events that object can raise then appear in the drop down to the right. Select the event and VBA writes the signature.
Note that you can only declare a withevents variable, and therefore you can only handle remote events, in a class module.
Cheers,
Thanks for the reply.
The application is registered and I successfully changed that application property from inside an access (2007) regular module using the syntax:
Dim CESApp As Ces.Application
Sub CES_Init()
Set CESApp = New Ces.Application
CESApp.XprobeMode = True
End Sub
However as far as the class module:
1) Is the name of the module important? If so what do I call it?
2) I put in the line:
Public WithEvents CESApp As Ces.Application
and when I open the dropdown box that says General then I see two entries:
CES
Class
When I select CES then access crashes with the message
Microsoft Office Access has encountered a problem and needs to close...
3) The event that CES generates is called Select. The documentation gives the following syntax:
Private Sub Application_Select(ByVal ObjType As Empty, ByVal ObjIdent As Empty, ByVal
ConsName As Empty, ByVal LevelName As Empty)
Should my class module look like this:
Option Compare Database
Option Explicit
Private WithEvents CESApp As Ces.Application
Sub CESapp_select(ObjType, ObjIdent, ConsName, LevelName)
MsgBox "Application - Select - object"
End Sub
Well, a form is a class module, if that helps. Or more accurately, the module behind a form is a class module.
And your code is not complete. You have failed to assign an instance of the CES.Application to the WithEvents variable. How do you get an instance? This example uses the New keyword...
Code:
Option Compare Database
Option Explicit
Private WithEvents CESApp As Ces.Application
private sub Class_Initialize()
[COLOR="Green"] 'assign a valid instance of CES to the WithEvents variable
'this must be the same instance that will raise the events you want to handle
[/COLOR] Set CESApp = New CES.Application
end sub
Sub CESapp_select(ObjType, ObjIdent, ConsName, LevelName)
[COLOR="Green"] 'with no 'As' clause, these parameters are implicity declared as variants[/COLOR]
MsgBox "Application - Select - object"
End Sub
I'm suspicious of the types of the parameters of the event since 'Empty' is not a VBA type, and the compiler can get pretty picky about the signature of an event handler, but test it. See what you get.
It's also not clear to me the context in which you are using CES. For instance, with Excel you might, from a button on a form, create an instance, present it to the user, and then handle events generated by the user's interaction with Excel. And you can handle those events, in Access, that were raised by Excel's objects.
Another common context is handling the events generated by an ActiveX control on a form.
So what class you use as a container for the CES application depends on what CES is for, how you need to use it, etc...
Cheers,