Run Access code from Excel

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi all

I've in middle of automating a database update for reporting.
I need to be able to run code (not a macro but actual VBA) in Access from within Excel.

This is what I have so far in Excel:-
Code:
Sub test()
    Dim conn As ADODB.Connection, rs As ADODB.Recordset, cmd As ADODB.Command, strConn As String

    file = Sheets("General_Info").Range("A5").Value
    Set conn = New ADODB.Connection
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & file & ";Persist Security Info=False;"
    conn.ConnectionString = strConn
    conn.Open
    conn.Execute cmdRemove5PMFigures_Click (this is the name of the Access procedure)
    Set cmd = New ADODB.Command
    
    cmd.CommandType = adCmdText
    cmd.ActiveConnection = conn
    
    setcmd = Nothing
    Set conn = Nothing
End Sub

When it gets to the conn.Execute line, I'm getting an error message which says:-
Command text was not set for the command object

Anyone any ideas where I'm going wrong?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hopefully this doesn't come across as a sort of "you don't want to do it like that, you want to do it like this" sort of post, but the only way I've ever managed to achieve what you're trying to do is as follows:-
Code:
  Dim iChannel As Integer
  Shell "msaccess c:\folder\database1.mdb", vbMinimizedNoFocus
  iChannel = DDEInitiate("MSACCESS", "system")
  Application.ActivateMicrosoftApp xlMicrosoftAccess
  Application.DDEExecute iChannel, "mcrNameOfMacro"
  Application.DDETerminate iChannel
This only works for macros, so you'd have to create a macro which runs the VBA code.

Don't know if this is any help...
 
Upvote 0
All help is gratefully received, so no, it doesn't come across wrong.
Is DDE the only way to do it?
Do I need any sort of references in the Tools menu to run this?
 
Last edited:
Upvote 0
Just the Microsoft Access Object Library, which you probably have already.

I really don't know if DDE is the only method - I was scrabbling around desperately at the time and I was just grateful I found something which worked!

I've just checked and you need to end your Access macro with a Quit command otherwise Access stays active. The other thing I do is use fAccessWindow to minimise Access as soon as the macro starts otherwise it stays on top whilst it's running.

fAccessWindow is third-party: Google will point you at the code (http://www.google.co.uk/search?q=faccesswindow).

What I'm not 100% sure about is whether you have to wait in some way for the Access macro to end before you can allow your Excel code can continue if you need to use the results of the macro.
 
Last edited:
Upvote 0
OK, thanks.
Just looking at your code, does this execute a macro or a sub-procedure (i.e. VBA code)? I'm after the latter if posible.
 
Upvote 0
What I'm not 100% sure about is whether you have to wait in some way for the Access macro to end before you can allow your Excel code can continue if you need to use the results of the macro.
No, I checked: the DDEExecute command appears to wait for the Access macro to finish executing before allowing program execution to proceed. So that's good.

Just looking at your code, does this execute a macro or a sub-procedure (i.e. VBA code)? I'm after the latter if posible.
I only ever managed to get it to execute an actual Access macro, not a query or VBA code directly, so I simply created a macro which just did an OpenQuery. You would have to create an Access macro which did a RunCode.

I'll have a play if I get a moment...
 
Upvote 0
MSDN states that DDEExecute passes "a string expression specifying a command recognized by the server application" (http://msdn.microsoft.com/en-us/library/aa221180(v=office.11).aspx).

The name of an Access macro is evidently "a command recognized by the server application", whereas the name of a VBA procedure or query is not... unless we just don't know how to present those names to Access in an acceptable format...

When you try to pass a function name in the DDEExecute command, Access warns: "If you are trying to call a user-defined VBA function, be sure to use the following syntax: =FunctionName() or =FunctionName(argument1,argument2,...)". I can't get that to work either.

I shall do a little more poking about when I get home tonight, if only to satisfy my own curiosity, but you may have to consider using a macro for the moment.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top