Ms access 将特定文件从文件夹导入Access表

Ms access 将特定文件从文件夹导入Access表,ms-access,import,vba,Ms Access,Import,Vba,背景: 我收到一份每日销售文件,希望自动导入access。它们当前保存到具有一致命名约定的特定文件夹中。我不是每天都查看这些文件,我想让导入过程成为一个按钮过程。文件夹中还有其他我不需要的文件,所以我不能只导入整个文件 文件命名约定:DAILY.SALES.20160611 (20160611是2016年、6月和11日) 需要帮助: 我可以导入所有文件,但我不知道如何只指定那些以“Daily.Sales”开头的文件。下面是我拥有的代码,可以导入所有内容而无需指定。我的假设是它与路径或strFil

背景:

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

文件命名约定: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
Dim strFile As String'文件名
Dim strFileList()作为字符串文件数组
Dim intFile作为“整数”文件号
将文件名设置为字符串
将路径设置为字符串
DoCmd.SetWarnings错误
path=“C:\Desktop\Test\”
作为对象的Dim objXL
作为对象的Dim wb
设置objXL=CreateObject(“Excel.Application”)
strFile=Dir(路径和“*.xls”)
而strFile“”
设置wb=objXL.Workbooks.Open(路径和strFile)
如果工作分解表(1)范围(“A1”)“无数据”和工作分解表(1)范围(“A1”)”,则
'将文件添加到列表中
intFile=intFile+1
ReDim保留strFileList(1到intFile)
strFileList(intFile)=strFile
如果结束
strFile=Dir()
调试.打印strFileList(intFile)
wb.关闭错误
设置wb=Nothing
温德
'查看是否找到任何文件
如果intFile=0,则
MsgBox“未找到任何文件”
出口接头
如果结束
'循环浏览文件列表
对于intFile=1到UBound(strFileList)
filename=路径和strFileList(intFile)
DoCmd.transfer电子表格导入,acSpreadsheetTypeExcel8,“阶段”,文件名,False
调用格式\u暂存\u表
将副本\u从\u Stage\u调用到\u Master
调用Clear\u Staging\u表
下一个intFile
DoCmd.SetWarnings True
你可以忽略调用片段,它们将在我收到数据后格式化


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

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


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

您想这样做吗
strFile=Dir(path&“Daily.Sales*.xls”)
“我尝试过的任何变体都不管用”-很难理解您尝试过的内容-几乎任何DOS文件seacrh规范都会管用-@HansUp建议很简单,也很管用-
“Daily.Sales.*.xls”和
“Daily.Sales.???.xls”
,然后您说“如果代码能够在上传之前检查文件是否已经上传就好了”-您使用的是MS Access-这是一个数据库,我想您使用的不仅仅是传输spreadhseets?它将通过简单的日志文件表和更新记录集以及检查它是否在表中的查询轻松处理此问题。