A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

How to Pass a Parm from VBS script to Access 2007



 
 
Thread Tools Display Modes
  #1  
Old April 21st, 2010, 02:59 AM posted to microsoft.public.access
Brad
external usenet poster
 
Posts: 943
Default How to Pass a Parm from VBS script to Access 2007

We have a VBS Script that initiates a Sub in an Access 2007 DB.

This works nicely.

We now would like to pass a parm from our VBS script to this Sub. It is our
understanding that the "command" field in our VBA code will contain the
passed data.

What we can't figure out is how to code the VBS script to pass the parm to
Access.

An example would be most appreciated.

Thanks,

Brad
  #2  
Old April 21st, 2010, 04:21 AM posted to microsoft.public.access
Tom van Stiphout[_2_]
external usenet poster
 
Posts: 1,653
Default How to Pass a Parm from VBS script to Access 2007

On Tue, 20 Apr 2010 18:59:01 -0700, Brad
wrote:

The way I understand it:
Your vba script invokes the db like this:
path_tomsaccess.exe path_toyour.mdb /cmd myCommand
In your startup code in the Access application, you can use the
Command function:
select case Command()
case "this"
'TODO: run this code
case "that"
'TODO: run that code
end select

-Tom.
Microsoft Access MVP


We have a VBS Script that initiates a Sub in an Access 2007 DB.

This works nicely.

We now would like to pass a parm from our VBS script to this Sub. It is our
understanding that the "command" field in our VBA code will contain the
passed data.

What we can't figure out is how to code the VBS script to pass the parm to
Access.

An example would be most appreciated.

Thanks,

Brad

  #3  
Old April 21st, 2010, 05:17 AM posted to microsoft.public.access
Tony Toews [MVP]
external usenet poster
 
Posts: 3,776
Default How to Pass a Parm from VBS script to Access 2007

Tom van Stiphout wrote:

The way I understand it:
Your vba script invokes the db like this:
path_tomsaccess.exe path_toyour.mdb /cmd myCommand
In your startup code in the Access application, you can use the
Command function:


The problem being that the VBS script likely doesn't know the location
of msaccess.exe and/or the location of msaccess.exe might vary from
system to system or from system to Terminal Server.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/
Granite Fleet Manager http://www.granitefleet.com/
  #4  
Old April 21st, 2010, 02:43 PM posted to microsoft.public.access
Brad
external usenet poster
 
Posts: 943
Default How to Pass a Parm from VBS script to Access 2007

Tom and Tony,

Sorry, I didn't ask my question very well.

We are getting hung up on the syntax of the VBS Script.

We have the Windows Scheduler fire up a little VBS Script which
in turn fires up a test Access 2007 DB. This works nicely.

We know how to work with the "Command" field once it gets to Acesss.

What we can't figure out is how to code the parameter (that we want to pass
to Access) in the VBS Script that fires up the Access application.

Brad


"Tony Toews [MVP]" wrote:

Tom van Stiphout wrote:

The way I understand it:
Your vba script invokes the db like this:
path_tomsaccess.exe path_toyour.mdb /cmd myCommand
In your startup code in the Access application, you can use the
Command function:


The problem being that the VBS script likely doesn't know the location
of msaccess.exe and/or the location of msaccess.exe might vary from
system to system or from system to Terminal Server.

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/
Granite Fleet Manager http://www.granitefleet.com/
.

  #5  
Old April 21st, 2010, 04:58 PM posted to microsoft.public.access
Hans Up
external usenet poster
 
Posts: 284
Default How to Pass a Parm from VBS script to Access 2007

Brad wrote:
We know how to work with the "Command" field once it gets to Acesss.

What we can't figure out is how to code the parameter (that we want to pass
to Access) in the VBS Script that fires up the Access application.


I experimented with this and I'll show you what I came up with, but not
sure it's what you're after.

My VBS script:

Dim objShell
Dim strExe
Dim strDb
Dim strParam

Set objShell = WScript.CreateObject("WScript.Shell")
strExe = "C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"
strDb = "C:\Access\wip\version_control\vc.mdb"
strParam = "Hi!"
ObjShell.exec(strExe & " " & strDb & " /cmd " & strParam)
Set ObjShell = Nothing

I have a startup form assigned in my database. So I put this in the
form's open event:

Private Sub Form_Open(Cancel As Integer)
If Len(Command()) 0 Then
MsgBox "Started with '" & Command() & "'"
End If
End Sub

Tom's Select Case suggestion offers more interesting possibilities.

If this is not helpful, please show us your VBS to help us understand
what you want.
  #6  
Old April 21st, 2010, 08:52 PM posted to microsoft.public.access
Brad
external usenet poster
 
Posts: 943
Default How to Pass a Parm from VBS script to Access 2007

Hans,

Here is what we are trying to do.

1. Use the Windows Scheduler to fire up a Windows Script (VBS).

2. Have this Windows Script fire up a specific SUB in an Access 2007
Database and pass in a parm that this SUB can access.

Brad


"Hans Up" wrote:

Brad wrote:
We know how to work with the "Command" field once it gets to Acesss.

What we can't figure out is how to code the parameter (that we want to pass
to Access) in the VBS Script that fires up the Access application.


I experimented with this and I'll show you what I came up with, but not
sure it's what you're after.

My VBS script:

Dim objShell
Dim strExe
Dim strDb
Dim strParam

Set objShell = WScript.CreateObject("WScript.Shell")
strExe = "C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"
strDb = "C:\Access\wip\version_control\vc.mdb"
strParam = "Hi!"
ObjShell.exec(strExe & " " & strDb & " /cmd " & strParam)
Set ObjShell = Nothing

I have a startup form assigned in my database. So I put this in the
form's open event:

Private Sub Form_Open(Cancel As Integer)
If Len(Command()) 0 Then
MsgBox "Started with '" & Command() & "'"
End If
End Sub

Tom's Select Case suggestion offers more interesting possibilities.

If this is not helpful, please show us your VBS to help us understand
what you want.
.

  #7  
Old April 21st, 2010, 09:13 PM posted to microsoft.public.access
Hans Up
external usenet poster
 
Posts: 284
Default How to Pass a Parm from VBS script to Access 2007

Brad wrote:
Hans,

Here is what we are trying to do.

1. Use the Windows Scheduler to fire up a Windows Script (VBS).

2. Have this Windows Script fire up a specific SUB in an Access 2007
Database and pass in a parm that this SUB can access.


Is it the same SUB every time? Or do you need to call it with one SUB
one time and a different SUB the next time?
  #8  
Old April 21st, 2010, 11:10 PM posted to microsoft.public.access
Brad
external usenet poster
 
Posts: 943
Default How to Pass a Parm from VBS script to Access 2007

Hans,

Same Sub each time, but the Parameter that we would like to pass from the
Windows Script to this Sub in Access may have a different value each time.

Thanks!

Brad


"Hans Up" wrote:

Brad wrote:
Hans,

Here is what we are trying to do.

1. Use the Windows Scheduler to fire up a Windows Script (VBS).

2. Have this Windows Script fire up a specific SUB in an Access 2007
Database and pass in a parm that this SUB can access.


Is it the same SUB every time? Or do you need to call it with one SUB
one time and a different SUB the next time?
.

  #9  
Old April 22nd, 2010, 01:19 AM posted to microsoft.public.access
Hans Up
external usenet poster
 
Posts: 284
Default How to Pass a Parm from VBS script to Access 2007

Brad wrote:
Hans,

Same Sub each time, but the Parameter that we would like to pass from the
Windows Script to this Sub in Access may have a different value each time.


OK, the same Sub each time would make it simpler. But while I was
waiting I tried an approach inspired by Tom's suggestion to offer more
flexibility.

You need something which fires when the database starts to accept the
parameter you're feeding with the /cmd switch. In my first try, I used
my form's open event because I already had that set up and I have little
experience with macros.

You could use an autoexec macro, but I chose to create a macro I called
"mcrStartController" and trigger it with the /x command line switch.

The macro consists of a single RunCode line, and the Function Name box
contains Controller()

Essentially all the Controller function does is break the strings out
from the Command() function and feed parameters to the appropriate sub:

Public Function Controller()
Dim varArguments As Variant
Dim i As Integer
Dim strMsg As String
varArguments = Split(Command())

Select Case varArguments(0)
Case "YourSub"
YourSub varArguments(1)
Case "DoubleIt"
DoubleIt varArguments(1)
Case Else
'log this if nobody will be around for the MsgBox
strMsg = "'" & varArguments(0) & "' not usable"
MsgBox strMsg
End Select
End Function

And here are two subs which can be called from Controller:

Public Sub YourSub(ByVal pstrVbsParam)
Dim strMsg As String
strMsg = "Hello " & pstrVbsParam
MsgBox strMsg
End Sub

Public Sub DoubleIt(ByVal pstrNumber As Double)
MsgBox pstrNumber & " * 2 = " & CStr(Val(pstrNumber) * 2)
End Sub

And this is the VBS which starts everything:

Dim objShell
Dim strExe
Dim strDb
Dim strParam
Dim strMacro

Set objShell = WScript.CreateObject("WScript.Shell")
strExe = "C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"
strDb = "C:\Access\wip\version_control\vc.mdb"
strMacro = "mcrStartController"

'strParam = "YourSub World"
strParam = "DoubleIt 5.2"
'strParam = "Deliberate failure here"

ObjShell.exec(strExe & " " & strDb & " /x " & strMacro & _
" /cmd " & strParam)
Set ObjShell = Nothing

It seems kind of fiddly, but it works. I couldn't see a simpler route
to get there. In your case, since you're dealing with only one sub, you
can simplify this thing.

I'm curious how you will feed it different parameters each time. Will
you modify the VBS script to change parameters values, or have you
worked out a slick alternative?
  #10  
Old April 22nd, 2010, 06:37 PM posted to microsoft.public.access
Brad
external usenet poster
 
Posts: 943
Default How to Pass a Parm from VBS script to Access 2007

Hans,

Thanks for the help and the great examples.

We haven't figured everything out yet, but I believe that we are one step
closer.

We are in the process of automating the running of several reports during
"off hours".

Your assistance will help us put the foundation pieces in place.

Thanks again,

Brad


"Hans Up" wrote:

Brad wrote:
Hans,

Same Sub each time, but the Parameter that we would like to pass from the
Windows Script to this Sub in Access may have a different value each time.


OK, the same Sub each time would make it simpler. But while I was
waiting I tried an approach inspired by Tom's suggestion to offer more
flexibility.

You need something which fires when the database starts to accept the
parameter you're feeding with the /cmd switch. In my first try, I used
my form's open event because I already had that set up and I have little
experience with macros.

You could use an autoexec macro, but I chose to create a macro I called
"mcrStartController" and trigger it with the /x command line switch.

The macro consists of a single RunCode line, and the Function Name box
contains Controller()

Essentially all the Controller function does is break the strings out
from the Command() function and feed parameters to the appropriate sub:

Public Function Controller()
Dim varArguments As Variant
Dim i As Integer
Dim strMsg As String
varArguments = Split(Command())

Select Case varArguments(0)
Case "YourSub"
YourSub varArguments(1)
Case "DoubleIt"
DoubleIt varArguments(1)
Case Else
'log this if nobody will be around for the MsgBox
strMsg = "'" & varArguments(0) & "' not usable"
MsgBox strMsg
End Select
End Function

And here are two subs which can be called from Controller:

Public Sub YourSub(ByVal pstrVbsParam)
Dim strMsg As String
strMsg = "Hello " & pstrVbsParam
MsgBox strMsg
End Sub

Public Sub DoubleIt(ByVal pstrNumber As Double)
MsgBox pstrNumber & " * 2 = " & CStr(Val(pstrNumber) * 2)
End Sub

And this is the VBS which starts everything:

Dim objShell
Dim strExe
Dim strDb
Dim strParam
Dim strMacro

Set objShell = WScript.CreateObject("WScript.Shell")
strExe = "C:\Program Files\Microsoft Office\OFFICE11\MSACCESS.EXE"
strDb = "C:\Access\wip\version_control\vc.mdb"
strMacro = "mcrStartController"

'strParam = "YourSub World"
strParam = "DoubleIt 5.2"
'strParam = "Deliberate failure here"

ObjShell.exec(strExe & " " & strDb & " /x " & strMacro & _
" /cmd " & strParam)
Set ObjShell = Nothing

It seems kind of fiddly, but it works. I couldn't see a simpler route
to get there. In your case, since you're dealing with only one sub, you
can simplify this thing.

I'm curious how you will feed it different parameters each time. Will
you modify the VBS script to change parameters values, or have you
worked out a slick alternative?
.

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 06:53 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.