Solved

Run macro via /x

Posted on 2009-04-02
11
702 Views
Last Modified: 2012-05-06
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
Comment
Question by:_Knocks_
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 57
ID: 24053271
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 24053289
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
 

Author Comment

by:_Knocks_
ID: 24053432
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 24053468
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
 

Author Comment

by:_Knocks_
ID: 24053541
Good suggestion sirbounty, I'll go and work on that!
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 57
ID: 24054055
<<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
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 24054057
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
 
LVL 57
ID: 24054076
Actually I just noticed I switched that around (1m instead of m1), but you get the idea.
JimD.
0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 24054297
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
 

Author Comment

by:_Knocks_
ID: 24057128
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
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 24059717
<< 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now