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

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.
0
Caltor
Asked:
Caltor
  • 2
1 Solution
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Can you post the code you're currently using? Also, what exactly are you trying to accomplish with the callback?
0
 
CaltorAuthor Commented:
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.eventhandler")
   
    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+"."+tcMethod+" "+"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("ApplicationEvents")
            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.log",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.

0
 
CaltorAuthor Commented:
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.
0
 
ee_ai_constructCommented:
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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