Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24

    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

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    You should find this link helpful: http://www.mvps.org/access/api/api0004.htm

  3. #3
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24
    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).

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    What is the full path to your .bat file?

  5. #5
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24
    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...

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Then you are going to call it with: Call ShellWait("C:\Local Drive\directory.bat")

  7. #7
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24
    I apologize for my ignorance, but how should i place it in VBA? I don't see anything in this code that says "Call".

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Start by creating a Standard Module named basAPI and copy the link intom it.

  9. #9
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24
    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)

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    And the module is named basAPI right? Do you know how to run a function from a macro and pass it an argument?

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    I had to change the ShellWait SUB to a FUNCTION so I could use RunCode to call it.

  12. #12
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24
    Unfortunately, I do not. Can you point me in the right direction? Thanks for your help with this.

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    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.

  14. #14
    NoiCe is offline Novice
    Windows XP Access 2003
    Join Date
    Nov 2008
    Posts
    24
    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

  15. #15
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    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

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Executing Access macro
    By Gnorro in forum Access
    Replies: 2
    Last Post: 09-21-2009, 08:32 AM
  2. Replies: 32
    Last Post: 09-16-2009, 10:06 AM
  3. Replies: 0
    Last Post: 09-11-2006, 07:11 AM
  4. Replies: 0
    Last Post: 04-24-2006, 06:48 AM
  5. How get path of executing database?
    By mscertified in forum Forms
    Replies: 3
    Last Post: 11-09-2005, 03:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums