【问题标题】:Import Specific Files from Folder into Access Table将特定文件从文件夹导入访问表
【发布时间】:2016-12-14 19:41:06
【问题描述】:

背景:

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

文件命名约定: DAILY.SALES.20160611

(20160611 是年份 - 2016,月份 - 六月和第 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

你可以忽略调用部分,一旦我得到它,它们就会格式化数据......

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

【问题讨论】:

  • 你想要这个吗? strFile = Dir(path &amp; "Daily.Sales*.xls")
  • “我尝试过的所有变体都没有奏效”-很难理解您尝试过的内容-几乎任何 DOS 文件 seacrh 规范都可以使用-@HansUp 建议很简单并且有效- "Daily.Sales.*.xls""Daily.Sales.????????.xls" 也是如此 - 然后你说“如果代码实际上可以在上传之前检查文件是否已经上传,那就太好了” - 你正在使用 MS-Access - 这是一个我假设你的数据库'用于做的不仅仅是传输电子表格?它可以通过简单的日志文件表和更新记录集以及检查它是否在表中的查询轻松处理。

标签: ms-access import vba


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 2017-12-24
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    • 2021-05-26
    • 2011-06-30
    • 1970-01-01
    相关资源
    最近更新 更多