Run acces macro in vba code

Edi110787

New Member
Joined
Nov 27, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Helo,

I use the following vba code to run a macro from an access database, but I get the error "Run-time error '2501' : the run macro action was canceled".

Dim A As Object
Set A = CreateObject("Access.Application")
A.Visible = True
A.OpenCurrentDatabase ("C:\D\Raport macro reconciliere\Raport Reconciliere.accdb")
A.DoCmd.RunMacro "Macro"
A.CloseCurrentDatabase

End Sub

Thank you very much
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It's odd to me that you would use code to run an Access macro rather than using code to run Access code. Anyway, step through your macro and see what's happening. It's likely that it is cancelling itself because of a conditional setting, or it's raising an error. That's the issue with Access macros - they don't have much in the way of error handling capability.
 
Upvote 0
It's odd to me that you would use code to run an Access macro rather than using code to run Access code. Anyway, step through your macro and see what's happening. It's likely that it is cancelling itself because of a conditional setting, or it's raising an error. That's the issue with Access macros - they don't have much in the way of error handling capability.
The access macro runs 100% manually with no issues.
 
Upvote 0
I think I missed the mark. Unless the two apps are asynchronous (that I don't know) then as soon as you command to run the macro you are closing the db. Comment out the close line and see what happens.
 
Upvote 0
I think Micron is right on. There is nothing in the code telling it to wait for the macro to finish running before closing the database.

Maybe make a version of the Macro that ends with a "Close database" command - so it is part of the Access Macro you are calling, and remove it from your Excel VBA code.
Then you won't run into this problem.

Alternatively, another method would be to add a pause to your Excel VBA code before the database closure step. You just need to be sure to choose some ample amount of time to ensure that your Access Macro finishes first.
See: Make VBA Code Pause or Delay (Using Sleep / Wait Commands) - Trump Excel.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,131
Messages
6,123,223
Members
449,091
Latest member
jeremy_bp001

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