Run Access Macro from VB Script

abesimpson

Active Member
Joined
May 3, 2003
Messages
435
For a variety of reasons I would like to start Access and various macros from a Visual Basic Script (VBS), or similar.

I have found the following code on the internet, but obviously I can't get it to work

Code:
set objaccess = createobject("Access.application")
objaccess.opencurrentdatabase ("E:\AO_VERIFICATION\AO.mdb")
objaccess.run "AO" ' >> Macro name
objaccess.CloseCurrentDatabase
set objaccess=nothing

Error:...can't find procedure AO... The macro name is AO, and the access path is correct.

Any suggestions as to how to fix it?

Thanks as always

a
 
Last edited:
That is what I do currently. But the databases (plural) are very large and take considerable time to complete the process. My preference would be to be able to schedule db maintenance during the evening.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
This is directly from Help in Access. If you search for compactrepair, you should be able to find it.

Code:
Function RepairDatabase(strSource As String, _
        strDestination As String) As Boolean
        ' Input values: the paths and file names of
        ' the source and destination files.

    ' Trap for errors.
    On Error GoTo error_handler

    ' Compact and repair the database. Use the return value of
    ' the CompactRepair method to determine if the file was
    ' successfully compacted.
    RepairDatabase = _
        Application.CompactRepair( _
        LogFile:=True, _
        SourceFile:=strSource, _
        DestinationFile:=strDestination)

    ' Reset the error trap and exit the function.
    On Error GoTo 0
    Exit Function

' Return False if an error occurs.
error_handler:
    RepairDatabase = False

End Function

HTH
Roger
 
Upvote 0
Personally i dont like the compact and repair approach. I use this script to compact (assuming you have closed the database):

Code:
Dim tmpext, theFile

tmpext = ".tmp" 
theFile = "C:\Your DB.mdb"

Set WshShell = wscript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

CompactDB (theFile)


wscript.echo "Compacted " & dbPath

Set WshShell = Nothing
Set fso = Nothing



'Compact an access Database
Function CompactDB(dbPath)

wscript.echo "Compacting " & dbPath

Set fso1 = CreateObject("Scripting.FileSystemObject")
Set jro = CreateObject("Jro.JetEngine")
jro.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & ";Jet OLEDB:Database Password=", _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbPath & tmpext & ";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Password="
fso1.DeleteFile (dbPath)
fso1.MoveFile dbPath & tmpext, dbPath
Set jro = Nothing
Set fso1 = Nothing
End Function

Hope it helps
 
Upvote 0

Forum statistics

Threads
1,216,223
Messages
6,129,592
Members
449,520
Latest member
TBFrieds

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