Results 1 to 6 of 6
  1. #1
    vvwstcat is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2021
    Posts
    12

    how to Run Access marcos from Power Shell

    (I put this post on the Macros thread, but seems there is nobody interested in this problem. I therefore unsubscribed this thread and put it in the Access General Thread. I am a newbie, I dont know how to delete this thread, would some experts help me. thanks)



    I want to do some automation using power shell and MS Access.
    The objective is to use PowerShell to Call up MS Access, then run a particular macro that has been defined in a MS Access database.

    $mypath = (get-location).Path
    $myDB = $mypath + "\US StockDB.accdb"
    $MsAccess = New-Object -ComObject Access.Application
    $MsAccess.OpenCurrentDatabase($myDB)


    $MsAccess.Run("MacImpStock")

    I got error as below:
    Exception calling "Run" with "1" argument(s): "Microsoft Access cannot find the procedure 'MacImpStock.'"
    At line:1 char:1
    + $MsAccess.Run("MacImpStock")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: ( [], MethodInvocationException
    + FullyQualifiedErrorId : COMException


    The Access Macro details is shown in the attached file.
    Appreciate your expert advice. Many thanks.
    Attached Thumbnails Attached Thumbnails MacImpStock.JPG  
    Last edited by vvwstcat; 06-30-2021 at 09:58 AM. Reason: want to delete this thread, but don't know how

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    Never have seen code like that. What is it?
    Can suggest basic trouble shooting technique though - output the path string variable (or check it via your preferred method) and ensure it is what you think it is. Mostly I've worked on Excel from Access, but either way I'd use automation as opposed to whatever that is. Since the code is unfamiliar to me, I'll also take a wild guess that it should be
    .RunMacro "macroName" rather than .Run ("MacroName"). () are usually only used where a function return value is being assigned, or with the Call statement.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    FYI I'm deleting your duplicate thread and moving this one to the general forum per your desire. I don't get the sense that many people here know much about Power Shell, me among them. You might have better luck on a Power Shell forum.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    vvwstcat is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2021
    Posts
    12
    thanks Paul.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I have done this in VBScript. Adapting for your object variable, consider:

    Use Run for VBA Sub procedures. Example for a Sub that expects 3 arguments.

    $MsAccess.Run "HelloTest", "June", "Home", "Late"

    Use RunMacro for macros.

    $MsAccess.DoCmd.RunMacro "Macro"
    Last edited by June7; 07-01-2021 at 09:18 AM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    vvwstcat is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2021
    Posts
    12
    Hi June7,
    Thank you very much for providing the solution. [$MsAccess.DoCmd.RunMacro "Macro"] does not work, but it is very closed.
    I searched the internet and found the solution. Though I've got the solution, but I think it is very
    weird - how come MS did not unify the command syntaxes?!
    The correct syntax is:
    $MsAccess.Application.DoCmd.RunMacro ("Macro")

    Thanks a lot.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Problem with Shell from Access
    By pgaccess in forum Access
    Replies: 13
    Last Post: 03-09-2017, 03:54 PM
  2. Access Marcos - New to this
    By SC5138 in forum Access
    Replies: 2
    Last Post: 11-09-2016, 11:39 AM
  3. Calling Microsoft Power Shell from within Access
    By crowegreg in forum Programming
    Replies: 1
    Last Post: 11-18-2014, 02:59 PM
  4. Run Database while hiding Access Shell
    By Sarah88 in forum Programming
    Replies: 1
    Last Post: 08-19-2014, 10:11 AM
  5. how to use marcos?
    By Bala Preetha in forum Access
    Replies: 5
    Last Post: 11-19-2010, 03:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums