Link to home
Start Free TrialLog in
Avatar of _Knocks_
_Knocks_

asked on

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?
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Looks fine here.  I don't believe it is case sensitive, but as a test, try something simpler like calling the macro '1'.
JimD.
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

Avatar of _Knocks_
_Knocks_

ASKER

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
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.
Good suggestion sirbounty, I'll go and work on that!
<<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
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
Actually I just noticed I switched that around (1m instead of m1), but you get the idea.
JimD.
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
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
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial