How to Run Batch File from Access

G

Guest

I want to Run a Dos Batch File from Microsoft Access Form Command Button.

I tried the following code, but failed :
Function RunBatch()
Dim GetFile As String
GetFile = Me!Text2
'In the text2 field of the form, I will write the complete batch file path
RunBatch = call Shell(Environ$("COMSPEC") & "/C GetFile", vbNormalFocus)
End Function

I called that Function on Command Button and it fails, does not work, gives
error.

Please advise, what will be writing Code.
 
K

kiwibruce

Hi
I think your code should read
RunBatch = call Shell(Environ$("COMSPEC") & "/c c:\GetFile.BAT",
vbNormalFocus)

Not

RunBatch = call Shell(Environ$("COMSPEC") & "/C GetFile", vbNormalFocus)
You must fully qualify the path and extension
See the Dev's (One of the Access Gods) site
http://www.mvps.org/access/general/gen0015.htm for reference

Regards

Bruce
 
G

Guest

I have Checked again as below as your advised , but still it gives error :

Function RunBatch()
RunBatch = call Shell(Environ$("COMSPEC") & "/C C:\TEST1.BAT", vbNormalFocus)
End Function

Please advise any other sulution.
Irshad.
 
G

Guest

give a try on this
dim currentpath as string, batchName as string,stAppname as string,retval as
variant
currentpath=CurrentProject.path (i use ADP) / you can use the appropriate
path of mdb
batchName =".... .bat" 'your bat file
stAppname = currentpath & "\" & batchName
retval= shell(stAppname ,vbMinimizedNoFocus) ' vbMinimizedNoFocus=6
doevents:doevents
'(try also the following workaround to get some time from the system)
' Msgbox " your message indicating the bat run ok",vbinformation
'i do not know why but after this msgbox it works!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top