将特定文件从文件夹导入访问表

时间:2016-08-08 22:06:04

标签: ms-access import access-vba

背景

我收到了我想要自动导入访问权限的每日销售文件。它们当前保存到具有一致命名约定的特定文件夹中。我不会每天查看这些文件,并希望将导入过程设为按钮程序。文件夹中还有其他我不需要的文件,所以我不能只导入整个文件。

文件命名惯例: DAILY.SALES.20160611

(20160611年是2016年,月 - 6月和第11天)

需要帮助:

我可以导入所有文件,但我无法弄清楚如何只指定那些以“Daily.Sales”开头的文件。下面是我可以导入所有内容而无需指定的代码。我的假设是它与路径或strFile有关,但我尝试过的变化都没有用。

如果代码在上传之前实际检查文件是否已经上传,那将是很好的,但是,如果我必须在每次使用后删除该表并重新上传更容易的内容。

Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim filename As String
Dim path As String
DoCmd.SetWarnings False
path = "C:\Desktop\Test\"

   Dim objXL As Object
   Dim wb As Object

   Set objXL = CreateObject("Excel.Application")

   strFile = Dir(path & "*.xls")
   While strFile <> ""

       Set wb = objXL.Workbooks.Open(path & strFile)

       If wb.Sheets(1).Range("A1") <> "No Data" And wb.Sheets(1).Range("A1") <> "" Then
           'add files to the list
           intFile = intFile + 1
           ReDim Preserve strFileList(1 To intFile)
           strFileList(intFile) = strFile
       End If
       strFile = Dir()
Debug.Print strFileList(intFile)
       wb.Close False
       Set wb = Nothing
   Wend

'see if any files were found
If intFile = 0 Then
MsgBox "No files found"
Exit Sub
End If

'cycle through the list of files
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "Stage", filename, False

Call Format_Staging_Table
Call Copy_from_Stage_to_Master
Call Clear_Staging_Table

Next intFile
DoCmd.SetWarnings True

您可以忽略“呼叫”片段,它们会在我获取数据后格式化数据...

感谢任何人提供的任何帮助或建议!

1 个答案:

答案 0 :(得分:0)

好的,我刚刚在1分钟前回答了这样的问题。我想你应该看看这个链接。

http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpAllWktsSepTbl

只需修改它以满足您的需求......基本上......更改路径和文件夹以匹配您的文件名...