Microsoft Access Forums

Go Back   Microsoft Access Forums > Access Forums > Programming

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-07-2010, 08:10 AM
NoiCe NoiCe is offline Windows XP Access 2003 (version 11.0)
Novice
 
Join Date: Nov 2008
Posts: 18
NoiCe is on a distinguished road
Default Executing .BAT file from Access

Hello,

I have a database setup with a macro that imports a Dir.txt file that is originally created by a Dir.bat file that i created in a folder. I basically click on this .bat file that creates a directory of all files in that specific folder with a .tif extension and actually removes that extension once the dir.txt file is created.

I basically want to have Access run the file automatically instead of having someone remember to run it first. Another problem is that Access needs to wait until the .bat file is finished running. How can i accomplish this? I really don't have that much experience with VBA but I'm sure with some help from all of you I can accomplish this. I appreciate any help you can provide.

Thanks,

- Jay
Reply With Quote
  #2  
Old 02-07-2010, 08:55 AM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

You should find this link helpful: http://www.mvps.org/access/api/api0004.htm
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #3  
Old 02-07-2010, 10:14 AM
NoiCe NoiCe is offline Windows XP Access 2003 (version 11.0)
Novice
 
Join Date: Nov 2008
Posts: 18
NoiCe is on a distinguished road
Default

Hmm...ok so where would i enter in the name of the .bat exe and the actual folder location of that file.

Also, how would i add this to the existing Macro (i did not use VBA to create it).
Reply With Quote
  #4  
Old 02-07-2010, 10:19 AM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

What is the full path to your .bat file?
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #5  
Old 02-07-2010, 10:56 AM
NoiCe NoiCe is offline Windows XP Access 2003 (version 11.0)
Novice
 
Join Date: Nov 2008
Posts: 18
NoiCe is on a distinguished road
Default

It's actually at work...but let's just say it's C:\Local Drive\directory.bat...i can change it to the actual drive later on...
Reply With Quote
  #6  
Old 02-07-2010, 11:03 AM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

Then you are going to call it with: Call ShellWait("C:\Local Drive\directory.bat")
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #7  
Old 02-07-2010, 11:49 AM
NoiCe NoiCe is offline Windows XP Access 2003 (version 11.0)
Novice
 
Join Date: Nov 2008
Posts: 18
NoiCe is on a distinguished road
Default

I apologize for my ignorance, but how should i place it in VBA? I don't see anything in this code that says "Call".
Reply With Quote
  #8  
Old 02-07-2010, 12:12 PM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

Start by creating a Standard Module named basAPI and copy the link intom it.
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #9  
Old 02-07-2010, 01:22 PM
NoiCe NoiCe is offline Windows XP Access 2003 (version 11.0)
Novice
 
Join Date: Nov 2008
Posts: 18
NoiCe is on a distinguished road
Default

OK so i placed the following in it's own Module:
Code:
'***************** Code Start ******************
'This code was originally written by Terry Kreft.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code Courtesy of
'Terry Kreft
Private Const STARTF_USESHOWWINDOW& = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type
Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
    lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
    ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
    lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    PROCESS_INFORMATION) As Long
    
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    hObject As Long) As Long
    
Public Sub ShellWait(Pathname As String, Optional WindowStyle As Long)
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ret As Long
    ' Initialize the STARTUPINFO structure:
    With start
        .cb = Len(start)
        If Not IsMissing(WindowStyle) Then
            .dwFlags = STARTF_USESHOWWINDOW
            .wShowWindow = WindowStyle
        End If
    End With
    ' Start the shelled application:
    ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    ' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    ret& = CloseHandle(proc.hProcess)
End Sub
'***************** Code End ****************
  • Where would i place the Location and File Name of the .Bat file?
  • And how would I call this as part of my existing Macro which is not VBA based (it's part of the Macro Design in Access)
Reply With Quote
  #10  
Old 02-07-2010, 03:34 PM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

And the module is named basAPI right? Do you know how to run a function from a macro and pass it an argument?
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #11  
Old 02-07-2010, 03:48 PM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

I had to change the ShellWait SUB to a FUNCTION so I could use RunCode to call it.
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #12  
Old 02-07-2010, 05:16 PM
NoiCe NoiCe is offline Windows XP Access 2003 (version 11.0)
Novice
 
Join Date: Nov 2008
Posts: 18
NoiCe is on a distinguished road
Default

Unfortunately, I do not. Can you point me in the right direction? Thanks for your help with this.
Reply With Quote
  #13  
Old 02-07-2010, 06:08 PM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

Here's how you change the Sub to a Function:
Code:
Public Function ShellWait(Pathname As String, Optional WindowStyle As Long) As Long
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ret As Long
    ' Initialize the STARTUPINFO structure:
    With start
        .cb = Len(start)
        If Not IsMissing(WindowStyle) Then
            .dwFlags = STARTF_USESHOWWINDOW
            .wShowWindow = WindowStyle
        End If
    End With
    ' Start the shelled application:
    ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    ' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    ret& = CloseHandle(proc.hProcess)
    ShellWait = ret
End Function
Then select RunCode in your macro and it should let you search for the ShellWait() function by pressing the "..." button.
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #14  
Old 02-10-2010, 08:31 AM
NoiCe NoiCe is offline Windows XP Access 2003 (version 11.0)
Novice
 
Join Date: Nov 2008
Posts: 18
NoiCe is on a distinguished road
Default

Rural,

Is this the only place where i post the path name/file name? See below in green:

Code:
Public Function ShellWait(Pathname As String, Optional WindowStyle As Long) As Long
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ret As Long
    ' Initialize the STARTUPINFO structure:
    With start
        .cb = Len(start)
        If Not IsMissing(WindowStyle) Then
            .dwFlags = STARTF_USESHOWWINDOW
            .wShowWindow = WindowStyle
        End If
    End With
    ' Start the shelled application:
    ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    ' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    ret& = CloseHandle(proc.hProcess)
    ShellWait = ret
End Function
Reply With Quote
  #15  
Old 02-10-2010, 08:40 AM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

That is actually being passed to the Function when it is invoked.
Code:
Public Function ShellWait(Pathname As String, Optional WindowStyle As Long) As Long
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ret As Long
    ' Initialize the STARTUPINFO structure:
    With start
        .cb = Len(start)
        If Not IsMissing(WindowStyle) Then
            .dwFlags = STARTF_USESHOWWINDOW
            .wShowWindow = WindowStyle
        End If
    End With
    ' Start the shelled application:
    ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
    ' Wait for the shelled application to finish:
    ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    ret& = CloseHandle(proc.hProcess)
    ShellWait = ret
End Function
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
Reply

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing Access macro Gnorro Access 2 09-21-2009 06:32 AM
Problems opening Access MDB file with Access 2007. BillKol Security 32 09-16-2009 08:06 AM
(Another) There was an error executing the command cr33p Access 0 09-11-2006 05:11 AM
Problems with privilegdes when executing a rpt query admaldo Programming 0 04-24-2006 04:48 AM
How get path of executing database? mscertified Forms 3 11-09-2005 12:56 PM


All times are GMT -8. The time now is 02:41 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.