Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 726
  • Last Modified:

Run macro via /x

Hi,

I'm using access 2003.

I'm using the following code to open my database and after that I'll try to automatically run a macro called test;

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Dim sCmd
sCmd = Chr(34) & "C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" & Chr(34) & " " & Chr(34) & "C:\Users\paul tikken\WordMerge2008.mdb" & Chr(34) & _
" /wrkgrp " & Chr(34) & "C:\Users\paul tikken\Beveiliging.mdw" & Chr(34) & " /user administrator /pwd ******* /X test"
Set oExec = WshShell.Exec(sCmd)

Access will open as it should but when it comes to the macro access tells me that it cannot find the test macro. I've checked the spelling several times, seems to match.

What is the problem?
0
_Knocks_
Asked:
_Knocks_
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Looks fine here.  I don't believe it is case sensitive, but as a test, try something simpler like calling the macro '1'.
JimD.
0
 
sirbountyCommented:
Nothing stands out - but you do have some extra characters in your cmd...use this instead...

You might also try renaming the macro.
Or could you place it in an autoexec macro?
sCmd = Chr(34) & "C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE " & Chr(34) & "C:\Users\paul tikken\WordMerge2008.mdb" & Chr(34) & _
" /wrkgrp " & Chr(34) & "C:\Users\paul tikken\Beveiliging.mdw" & Chr(34) & " /user administrator /pwd ******* /X test"

Open in new window

0
 
_Knocks_Author Commented:
Jdettman; I tried to rename the macro to 1, it wouldn't take it (integer only) so I renamed it m1, still the same problem; access tells me it cannot find the macro. Do I need to refer to the module name as well? I can't find anything the documentation.

Sirbounty; those additonal charaters where neccessay because my path's have spaces in them, I tried your code, it didn't work at all.
Unfortunally I can't place it in a autoexec macro, because the macro I woul like to run is connected to the task scheduller and I only want the macro to run when tasked by the task scheduller, otherwise every end user who opens the database will trigger the macro, right?

cheers,

Paul
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sirbountyCommented:
Hmm - Jim would know better on that - not sure if you can limit it by username or not.
I would suspect so - grab the %username% environment variable - if it's SYSTEM, then launch the code, if not skip it...

I noticed the spaces in the paths, but apparently misread something somewhere...thought there was an extra space or two.
0
 
_Knocks_Author Commented:
Good suggestion sirbounty, I'll go and work on that!
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<Jdettman; I tried to rename the macro to 1, it wouldn't take it (integer only) so I renamed it m1, still the same problem; access tells me it cannot find the macro. Do I need to refer to the module name as well? I can't find anything the documentation.>>
No, no need for anything else, but I started thinking what you might mean by "module".
m1 should be the name of the macro in the database container. Access will not be able to find it if you used 'm1' in the macro name column inside of a marco. Is that what you meant by "module'?
JimD.

CorrectWay.jpg
Wrongway.jpg
0
 
Eric ShermanAccountant/DeveloperCommented:
Try this ...

Dim RetVal as double
Dim sCmd
sCmd = Chr(34) & "C:\Program Files\Microsoft Office\Office12\MSACCESS.EXE" & Chr(34) & " " & Chr(34) & "C:\Users\paul tikken\WordMerge2008.mdb" & Chr(34) & _
" /wrkgrp " & Chr(34) & "C:\Users\paul tikken\Beveiliging.mdw" & Chr(34) & " /user administrator /pwd ******* /X test"

RetVal = Shell(sCMD, vbMaximizedFocus)


ET
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Actually I just noticed I switched that around (1m instead of m1), but you get the idea.
JimD.
0
 
Eric ShermanAccountant/DeveloperCommented:
Knocks ...

I tried this is my test db and it worked.  Notice, I'm not inserting the Chr(34).

Function TestOpenWithMacro()
    Dim RetVal As Double
    Dim sCmd
    sCmd = "C:\Program Files (x86)\Microsoft Office\Office12\MSACCESS.EXE" & " " & "C:\MyFolder\MyDB.mdb" & _
    " /wrkgrp " & "C:\MyFolder\MySecured.mdw" & " /user ets /pwd password /X test"

    RetVal = Shell(sCmd, vbMaximizedFocus)


End Function

Hope this helps ...

ET
0
 
_Knocks_Author Commented:
I think I got my head around the problem, I was trying to run a macro written in VBA instead of in the macro section of the db, and I was trying to run that VBA macro. Now I've made a macro in the db and it works great!

Cheers
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<< I was trying to run a macro written in VBA instead >>
  Ah.  Well first, just to be clear on the terms, in VBA you write procedures, which are either subs (don't return a value) or function calls (return a value).
  and you can (and should) use VBA procedures whenever possible instead of a macro (marco's have no error trapping).
  You can do this by creating a macro with a single RunCode action and then name your VBA function.  Then from the command line call that macro with the /x switch.  Note that the VBA procedure must be a function when called from a macro.  It cannot be a sub.
JimD.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now