Solved

Run macro via /x

Posted on 2009-04-02
11
701 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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
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_
Comment Utility
Good suggestion sirbounty, I'll go and work on that!
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
<<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
Comment Utility
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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
<< 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

772 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