views:

123

answers:

1

Hi all,

I want my Access application to run an external program (in this case a R script) after the user clicks a button. I use this code:

Dim RetVal
RetVal = Shell("""C:\Program Files\R\R-2.10.1\bin\R.exe"" CMD BATCH --no-environ --silent --no-restore --no-save ""c:\test.R"" ""c:\test-result.txt""", vbHide)
MsgBox RetVal

This works fine. But:

  • The Access code goes on while my script is running. How can I make Access waiting for the script finishing?
  • Has anybody suggestions about how to give an error message of the script back to Access?

Thanks in advance.

A: 

The OpenProcess and WaitForSingleObject combo that @Remou links to, is probably your best bet for doing this. You should take a look at this, it's a nice drop in module for shell and wait.

For returning a message back from the script, you could mess around with redirecting the scripts input and output. This is not for the faint of heart. As an alternative I would redirect the output of the script to a text file, then read in that file after it exits.

Mark
@Remou meant the second option, the CreateObject("WScript.Shell").Run code. That's just 1 sentence of code, the Shell And Wait code at cpearson.com around 250 sentences. I'm not lazy, but ... ;-)
waanders
>Mark: Thanks, I'll consider the different options
waanders
I was trying to run the code @ http://www.cpearson.com/excel/ShellAndWait.aspx. But I think it's VBA written especially for Excel. I'm using Access VBA and it's starts complaining about XlEnableCancelKey ('Type not defined')
waanders
@waanders, good point, I should have read a little more carefully, you are correct there is A LOT of excel specific constants in there. Here's a "dumbed" down version without all the bells and whistles (and the Excel stuff): http://www.mvps.org/access/api/api0004.htm
Mark
Thanks. Unfortunately this ShellWait routine doesn't return a value indicating if the process completed successfully. I've to change it a bit
waanders