Caltor
asked on
Using callbacks from VBA/Access
I am following the instructions in http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfoxgen7/html/vfpandcom.asp
for using callbacks. It works absolutely fine from VFP8 but I need to use this method from Microsoft Access (vba). When I try the same thing from Access I get the following error message. ".SETCALLBACK: callback must be object." It would appear that the parameter being passed instead of the callback object is an empty character string.
Please help I really need to get this working from Access.
for using callbacks. It works absolutely fine from VFP8 but I need to use this method from Microsoft Access (vba). When I try the same thing from Access I get the following error message. ".SETCALLBACK: callback must be object." It would appear that the parameter being passed instead of the callback object is an empty character string.
Please help I really need to get this working from Access.
Can you post the code you're currently using? Also, what exactly are you trying to accomplish with the callback?
ASKER
Code is posted below. The idea of the callback is to allow me a core product COM/DLL and then create extensions using an additional COM/DLL. The two are then bound together using the callbacks.
The entire project is huge and very specific so I have created a sample of the problem.
You can download the entire thing from http://www.helixnet.co.uk/callback.zip
You will need to register proj1.dll and proj2.sll using regsvr32
eg regsvr32 proj1.dll
regsvr32 proj2.dll
After you have done this you can look at the db1.mdb that has a demo of the problem.
The problems are logged in c:\app.log
Alternatively if you like typing you can use all the code below....
************************** *****
Access Code attached to a command button
************************** *****
Option Compare Database
Private Sub Command1_Click()
Dim objCallback As Object
Dim objMain As Object
Set objMain = CreateObject("proj1.class1 ")
Set objCallback = CreateObject("proj2.eventh andler")
objMain.SetCallback (objCallback)
MsgBox ("Program completed!")
End Sub
Create a program called program2 in Visual Foxpro 8 and paste the following code
Define Class EventHandler As Session OlePublic
Implements Iapplicationevents In "proj1.ApplicationEvents"
Procedure Iapplicationevents_Event1( ) AS VARIANT
*-- Perform the callback code
StrToFile("Dummy File","c:\dummy.txt",0)
EndProc
EndDefine
Add this program to a project called proj2 and build into a COM/DLL
Create a program called program1 in vfp8 and paste the following code:
Define Class Class1 As Session OLEPublic
oCallback = .Null.
Function Error(tnError, tcMethod, tnLine)
Local lcMessage As String, lnObjectName As String
lcObjectName = This.Name
lcMessage = "ERROR: "+lcObjectName+"."+tcMetho d+" "+"Line: "+AllTrim(Str(tnLine))+" "+Message()
This.WriteLog(lcMessage)
EndFunc
Function Function1
*-- Do some pre callback code...
*-- Issue the callback
This.oCallback.Event1()
*-- Do some post callback code...
EndFunc
Function Init
This.WriteLog("Starting... ")
*-- Initialise Callback Object
This.WriteLog("Initialise callback object")
This.SetCallback(.Null.)
This.WriteLog("Callback object has been initialised")
EndFunc
Function SetCallback(toCallback As Object)
Local lcType As Character
If IsNull(toCallback)
*-- dummy instance that does nothing
This.oCallback = NewObject("ApplicationEven ts")
Else
*-- Check callback variable is an object
lcType = Vartype(toCallBack)
If lcType != 'O'
This.WriteLog(PROGRAM()+": callback must be object. Parameter passed is "+lcType)
EndIf
This.oCallback = GetInterface(toCallback, "IApplicationEvents", "proj1.class1")
EndIf
EndFunc
Function WriteLog(tcString)
*-- Write expression to default log file
*-- Prefix with date and time
*-- Suffix with file name
*-- Always append to file
StrToFile(Dtoc(Date())+" "+Time()+" - "+tcString+Chr(13)+Chr(10) ,"c:\app.l og",1)
EndFunc
EndDefine
Define Class ApplicationEvents As Session OLEPublic
*-- Template for Callback Interface --*
Function Event1()
EndDefine
Add this program to a project called proj1 and build into a COM/DLL
You will then be able to try the access sample above.
The entire project is huge and very specific so I have created a sample of the problem.
You can download the entire thing from http://www.helixnet.co.uk/callback.zip
You will need to register proj1.dll and proj2.sll using regsvr32
eg regsvr32 proj1.dll
regsvr32 proj2.dll
After you have done this you can look at the db1.mdb that has a demo of the problem.
The problems are logged in c:\app.log
Alternatively if you like typing you can use all the code below....
**************************
Access Code attached to a command button
**************************
Option Compare Database
Private Sub Command1_Click()
Dim objCallback As Object
Dim objMain As Object
Set objMain = CreateObject("proj1.class1
Set objCallback = CreateObject("proj2.eventh
objMain.SetCallback (objCallback)
MsgBox ("Program completed!")
End Sub
Create a program called program2 in Visual Foxpro 8 and paste the following code
Define Class EventHandler As Session OlePublic
Implements Iapplicationevents In "proj1.ApplicationEvents"
Procedure Iapplicationevents_Event1(
*-- Perform the callback code
StrToFile("Dummy File","c:\dummy.txt",0)
EndProc
EndDefine
Add this program to a project called proj2 and build into a COM/DLL
Create a program called program1 in vfp8 and paste the following code:
Define Class Class1 As Session OLEPublic
oCallback = .Null.
Function Error(tnError, tcMethod, tnLine)
Local lcMessage As String, lnObjectName As String
lcObjectName = This.Name
lcMessage = "ERROR: "+lcObjectName+"."+tcMetho
This.WriteLog(lcMessage)
EndFunc
Function Function1
*-- Do some pre callback code...
*-- Issue the callback
This.oCallback.Event1()
*-- Do some post callback code...
EndFunc
Function Init
This.WriteLog("Starting...
*-- Initialise Callback Object
This.WriteLog("Initialise callback object")
This.SetCallback(.Null.)
This.WriteLog("Callback object has been initialised")
EndFunc
Function SetCallback(toCallback As Object)
Local lcType As Character
If IsNull(toCallback)
*-- dummy instance that does nothing
This.oCallback = NewObject("ApplicationEven
Else
*-- Check callback variable is an object
lcType = Vartype(toCallBack)
If lcType != 'O'
This.WriteLog(PROGRAM()+":
EndIf
This.oCallback = GetInterface(toCallback, "IApplicationEvents", "proj1.class1")
EndIf
EndFunc
Function WriteLog(tcString)
*-- Write expression to default log file
*-- Prefix with date and time
*-- Suffix with file name
*-- Always append to file
StrToFile(Dtoc(Date())+" "+Time()+" - "+tcString+Chr(13)+Chr(10)
EndFunc
EndDefine
Define Class ApplicationEvents As Session OLEPublic
*-- Template for Callback Interface --*
Function Event1()
EndDefine
Add this program to a project called proj1 and build into a COM/DLL
You will then be able to try the access sample above.
ASKER
I have solved this problem myself with the help of Microsoft.
I changed the object types in Access to type Variant instead of type Object.
I changed the object types in Access to type Variant instead of type Object.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.