Run Batch File from MSAcces

  • Thread starter Pierced Indie Rock Band
  • Start date
P

Pierced Indie Rock Band

I am trying to run net user in a .bat file from MS Access.

For some reason I get stuck on the Shell line. If I run the .bat file
manually, it works perfectly.

How can I get this to work?

Code
Private Sub Command0_Click()
Dim dbs As Database
Dim rst As DAO.Recordset
Dim f
Dim userID As String

Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("qryData")

rst.MoveFirst
Do Until rst.EOF

userID = rst![Exchange Alias]
Open "C:\UserAccesReport\GetUser.bat" For Output As #1

Print #1, "net user " & userID & " /domain" >C:\Results\" & userID
& ".txt"
Close #1 ' Close file.

Shell "C:\UserAccesReport\GetUser.bat"

rst.MoveNext

Loop
MsgBox "Complete"
End Sub
 
D

Douglas J. Steele

What does "stuck" mean? What exactly is the problem?

Remember that Shell runs asynchronously. That means that it may not have
finished running by the time you move up to create the next version of the
bat file. If that's the problem you're running into, take a look at
http://www.mvps.org/access/api/api0004.htm at "The Access Web"
 
G

Guest

Sorry about that. My post was very non descriptive.

I got it to work by changing the path. I moved my folder and changed the
path and it worked perfectly.

I had it under "My Documents" and I'm guessing the space was throwing off
the code.

Thanks for the reply.

Brad

Douglas J. Steele said:
What does "stuck" mean? What exactly is the problem?

Remember that Shell runs asynchronously. That means that it may not have
finished running by the time you move up to create the next version of the
bat file. If that's the problem you're running into, take a look at
http://www.mvps.org/access/api/api0004.htm at "The Access Web"

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Pierced Indie Rock Band said:
I am trying to run net user in a .bat file from MS Access.

For some reason I get stuck on the Shell line. If I run the .bat file
manually, it works perfectly.

How can I get this to work?

Code
Private Sub Command0_Click()
Dim dbs As Database
Dim rst As DAO.Recordset
Dim f
Dim userID As String

Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("qryData")

rst.MoveFirst
Do Until rst.EOF

userID = rst![Exchange Alias]
Open "C:\UserAccesReport\GetUser.bat" For Output As #1

Print #1, "net user " & userID & " /domain" >C:\Results\" & userID
& ".txt"
Close #1 ' Close file.

Shell "C:\UserAccesReport\GetUser.bat"

rst.MoveNext

Loop
MsgBox "Complete"
End Sub
 
G

Guest

Thanks so much for your help Tony,

I've learned something new.

have a great day,

Brad
 

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