简体   繁体   中英

MS Access VBA and Macro to Export

I have a MS Acccess 2013 Database with many stored queries and linked tables to Excel spreadsheets. There are 3 in particular I need to export and I need to create backups as well. In my attempt to automate it, I am trying to use VBA.

Query Names:

query1
query2
query3

The DB is located in \\\\Reports\\Run\\Data I would like BOTH the first and second query to export to both \\\\Reports\\Type1\\ and \\\\Reports\\Type1\\[new sub-folder 1] I would like the third query to export to both \\\\Reports\\Type2\\ and \\\\Reports\\Type2\\[new sub-folder 2]

One of the linked Excel spreadsheets (Table Name = Sheet1) has 1 single field and 1 single entry, which is the ReportDate . I would like both [new sub-folder 1] and [new sub-folder 2] to be that single date entry. For example, if 2019-03-06 was the entry, both sub-folders should be called "2019-03-06". These are my backup and stored copies.

The export should overwrite the existing files in \\\\Reports\\Type1 and \\\\Reports\\Type2 .

It would be nice to be able to prefix the names the files in the new sub-folders with the ReportDate as well if possible.

So the final result would then be \\\\Reports\\Type1\\2019-03-06\\20190306_query1.xlsx , \\\\Reports\\Type1\\2019-03-06\\20190306_query2.xlsx and \\\\Reports\\Type2\\2019-03-06\\20190306_query3.xlsx as an example.

I created a macro to export and converted it to VBA as a starting point. However, I am not sure how to do the dynamic naming and changing the path dynamically of the export.

Here is the code I used. I hope it helps someone else!

Option Compare Database
Option Explicit

Public Function ExportExcel()

  'Declare all variables

  Dim file As Object

  Dim filepath As String
  Dim fp_report1 As String
  Dim fp_report2 As String
  Dim fp_report1_date As String
  Dim fp_report2_date As String
  Dim data1 As String
  Dim data2 As String
  Dim data3 As String
  Dim fp_report1_data1 As String
  Dim fp_report1_data2 As String
  Dim fp_report2_data3 As String
  Dim fp_reportdate As String
  Dim reportdate_backup As String
  Dim reportdate As String


  Dim data1_run As String
  Dim data2_run As String
  Dim data3_run As String

  Dim myVar As Date


  'Get report date from the ReportDate linked table
  myVar = DLookup("ReportDate", "tbl_reportdate", "ReportDate")

  ' Get current path
  filepath = CurrentProject.Path

  'Destination for files for dashboard
  fp_report1 = Left(filepath, 87) & "\report1\"
  fp_report2 = Left(filepath, 87) & "\report2\"

  'Location for backup with the date as the folder name
  fp_report1_date = Left(filepath, 87) & "\report1\" & Format(myVar, "yyyy-mm-dd")
  fp_report2_date = Left(filepath, 87) & "\report2\" & Format(myVar, "yyyy-mm-dd")

  'Location of raw reports to backup and date file
  fp_report1_data1 = Left(filepath, 97) & "report1_data1.xls"
  fp_report1_data2 = Left(filepath, 97) & "report1_data2.xls"
  fp_report2_data3 = Left(filepath, 97) & "report2_data3.xls"
  fp_reportdate = Left(filepath, 97) & "ReportDate.xlsx"

  'If the folders for the backup doesn't exist, create it, otherwise, do nothing.
  If Dir(fp_report1_date, vbDirectory) = "" _
  Then MkDir (fp_report1_date) _
  Else _

  If Dir(fp_report2_date, vbDirectory) = "" _
  Then MkDir (fp_report2_date) _
  Else _

  'Exact path with file name for backup of processed data in the appropriate date folder
  data1 = fp_report1_date & "\" & "data1.xlsx"
  data2 = fp_report1_date & "\" & "data2.xlsx"
  data3 = fp_report2_date & "\" & "data3.xlsx"
  reportdate_backup = fp_report1_date & "\" & "ReportDate.xlsx"

  'Exact path with file name for dashboard to automatically pull
  data1_run = fp_report1 & "data1.xlsx"
  data2_run = fp_report1 & "data2.xlsx"
  data3_run = fp_report2 & "data3.xlsx"
  reportdate = fp_report1 & "ReportDate.xlsx"

  'Export queries into the date backup folder
  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "data1", data1
  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "data2", data2
  DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "data3", data3

  'Copy the files from the date backup folder into the path to be used by the dashboard. This includes the raw data in the reports and all the processed reports
  FileCopy data1, data1_run
  FileCopy data2, data2_run
  FileCopy data3, data3_run
  FileCopy fp_report1_data1, fp_report1_date & "\" & "report1_data1.xls"
  FileCopy fp_report1_data2, fp_report1_date & "\" & "report1_data2.xls"
  FileCopy fp_report2_data3, fp_report2_date & "\" & "report2_data3.xls"
  FileCopy fp_reportdate, reportdate_backup
  FileCopy reportdate_backup, reportdate

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM