how to call a function in access from matlab

Joined
5/13/12
Messages
4
Points
11
Hello!

I want to call a function written in vba access from matlab. So far I have this.
Access = actxserver('Access.application');
Db = invoke(Access.DBEngine,'OpenDatabase', 'path.accdb');

and what i need is this (but from matlab)
Public Sub mm()
Dim appAccess As Access.Application
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase "project_path\mimi.mdb", False
xx = appAccess.Application.Run("mimi.multi", 3) 'mimi.multi is the project name and function name
appAccess.Quit
End Sub

Any advice??

Thanks
 
I have some small achievements. The proper method to call on the COM object is 'Run' (just as calling myaccessfun from an other database in access). If I write Access.methodsview in matlab, I can see all the methods supported, and fortunately 'Run' is there. To double check I can write :
e = Access.Application
e.ismethod('Run')
and the answer is 1, meaning that 'Run' is a method of my access.application.
So it is only matter to pass the 'Run' method correctly. This is the part that I am missing!. I have tried this options with no success
xx=invoke(Access.Application,'Run','myaccessfun',3)
xx=invoke(Access.Application.Run,'myaccessfun',3)

I can also open the database using this notation (which is consistent with the methods supported by Access.Appliaction) :
Access.Application.OpenCurrentDatabase('path.mdb')

Any idea?
 
I do this from not matlab but windows scheduler in a bat script, what you are missing is that the Run command is only going to run "macros" not vba subroutines. Go into the access database and create a macro which calls the vba subroutine and then it should work.
 
Thanks Patrickb!

You point me in the right direction. I have created the macro, but it doesnt work as I need. Firtly, I cannot create the macro in such a way that I can pass any value to the function. My function is access has one variable. If a write the value of the variable in the code (so not a function any more) then I can create the macro. That macro can be call from matlab but using the following notation:
invoke(appAccess.DoCmd,'RunMacro','MyMacro');
Do you know if I can create macros where i can pass the variable?
 
By the way.... the code below call the function from another database. But the database needs to be .mdb (it didnt work with for access2007 format)

Public Sub test()

Dim appAccess As Access.Application
Set appAccess = CreateObject("Access.Application")

appAccess.OpenCurrentDatabase "C:path\project_path.mdb", False
xx = appAccess.Application.Run("project_name.function_name", 3)

End Sub
 
Back
Top Bottom