This example shows how to store the object received when connecting to an ActiveX server. This is useful when you do not want to set the DLL as an event server. You use the normal way of handling events, and in the events you retrieve the shared object, and call a function inside. In this way you can use your DLL as a function library.
The first two functions are to be written into the FORMS VBA editor on appropriate events. The rest of the code is to be put in your VB DLL.
Function OnJobStarted() As Long
'------------------------------
'Connect to the ActiveX
'------------------------------
Dim VBObject As Object
'projectname is the name of
'the project in VB, class is the class name
Set VBObject = CreateObject("projectname.class")
' Call a function inside the DLL you have written
'to receive the Application object
VBObject.Connect (VerifyApp)
'Store the object inside the FORMS Application
VerifyApp.SetSharedObject ("DLLObject", VBObject)
OnJobStarted = EV_OK ' Return value to FORMS
End Function
Function OnFormComplete() As Long
'---------------------------------------------
' You can now call a function within the DLL
' by using the object stored
'---------------------------------------------
Dim DLL_Object As Object
' Get the shared object. FALSE means that the object
' should be kept in FORMS
Set DLL_Object = VerifyApp.GetSharedObject("DLLObject", FALSE)
' Call the function in the DLL
DLL_Object.CalculateFields
OnFormComplete = EV_OK ' Return value to FORMS
End Function
And now the code in the class module of the DLL:
'Declare a global variable
Dim Application As Object
Function Connect(EHApp As Object)
'--------------------------------------------------------
' This gives you the FORMS Application object in the DLL, as well.
' It is needed to access all functions and objects in the FORMS module.
'--------------------------------------------------------
Set Application = EHApp
End Function
Function CalculateFields()
'---------------------------------------------------------------------
' The function calculates the sum of all fields (which are
' all supposed to be numeric).
'---------------------------------------------------------------------
Dim NoOfFormFields As Integer
Dim I As Integer
Dim Sum As Long
' Get how many fields there are on the form
NoOfFormFields = Application.Form.Fields.Count
For I = 1 To NoOfFormFields ' Step through them
'One way to get the value from field
Sum = Sum + Val(Application.Form.Fields.Item(I).Value)
Next I
If Sum > 20000 Then
MsgBox "This client is a big spender. " & _
"Put him in the priority queue"
End If
End Function