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?
 
For interest, "The DoCmd object doesn't support methods corresponding to the following actions... RunCode. Run the function directly in Visual Basic." (http://msdn.microsoft.com/en-us/library/aa223114(v=office.11).aspx)

How you do that I don't know!

To run a query:-
Code:
Option Explicit
Sub RunAccessQuery()
  Dim iChannel As Integer
 
  Shell "MSAccess " & "c:\folder\database1.mdb", vbMinimizedNoFocus
  
  iChannel = DDEInitiate("MSAccess", "System")
  
  Application.DDEExecute iChannel, "[OpenDatabase " & dbPath & "scratch_database1.mdb]"
  Application.DDEExecute iChannel, "[SetWarnings 0]"
  Application.DDEExecute iChannel, "[OpenQuery qmakMyNewTable]"
  Application.DDEExecute iChannel, "[CloseDatabase]"
  Application.DDEExecute iChannel, "[Quit]"
  
  Application.DDETerminate iChannel
 
End Sub
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi Ruddles, I've come across a small flaw with this code and can't seem to sort it.
Code:
Sub Process_Data()
    Dim db As Object
    file = Sheets("General_Info").Range("A5").Value

    Set db = CreateObject("Access.Application")
    db.Visible = True
    db.opencurrentdatabase (file)
    db.docmd.runmacro "mcr_Process_Data"
    db.Quit
    Set db = Nothing
End Sub

It's not releasing the database lock (the ldb file) when it's finished. It only releases when I close the spreadsheet.
Any ideas?
 
Upvote 0
I've run your code and the LDB gets deleted okay when the db.Quit command executes.

I suppose you could try a db.CloseCurrentDatabase before the db.Quit.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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