title: Run Access VBA in all Access files in folder
body: <p>I have been trying to figure out a way to run the following VBA code on all of the files I have in a folder without having to manually open each file.</p>
  
  <p>This is the code I have right now (exports the desired table as a delimited txt file, including column names):</p>
  
  <pre><code>Private Sub Command4_Click()
  
      Dim MyObj, MySource As Object, File As Variant, stDocName As String, Counter As Integer
  
      On Error GoTo Err_Command4_Click
          Dim stDocName As String, Counter As Integer
          Counter = 1
          stDocName = "tblSCTurCount"
          DoCmd.TransferText acExportDelim, "", stDocName, "C:\Users\name\Downloads\cnt\cnt_output.txt", True
  
      Exit_Command4_Click:
          Exit Sub
  
      Err_Command4_Click:
          MsgBox Err.Description
          Resume Exit_Command4_Click
  
  End Sub
  </code></pre>
  
  <p>When researching the problem, I found <a href="https://stackoverflow.com/questions/14766238/run-same-excel-macro-on-multiple-excel-files">a process</a> that works in excel, but I'm not sure how to do the variables change in access, especially the workbook references.</p>
  
  <p>Thank you!</p>
  
  <p>EDIT -- Code that worked:</p>
  
  <pre><code>Dim FS As FileSystemObject
  Set FS = New FileSystemObject
  Dim MyFolder As Folder
  Set MyFolder = FS.GetFolder("C:\Users\name\Downloads\cnt\Folder")
  Dim MyFile As File
  Set appAccess = CreateObject("Access.Application")
  
  For Each MyFile In MyFolder.Files
      appAccess.OpenCurrentDatabase (MyFile.Path)
      appAccess.Visible = True
      NewFileName = MyFile.Path &amp; ".txt"
      appAccess.DoCmd.TransferText acExportDelim, "", "tblScTurCount", NewFileName, True
      appAccess.CloseCurrentDatabase
  Next
  </code></pre>