Brad said:
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?