将具有变量名称的日常文件导入访问数据库
Importing a daily file with variable name into access database
如果有人在某个地方提出这个问题而我忽略了它,请让我提前道歉。我在这上面花了很多天,但无法达到 运行 100%。
我正在尝试将每天早上通过电子邮件发送的 excel 文件导入到访问数据库中。该文件的日期部分每天都在变化。命名每天都遵循相同的模式 "FTTQ m-dd-yyyy"。文件名中显示的日期是前一个工作日,例如。 8 月 25 日收到 8 月 24 日 FTTQ 的电子邮件。下面的代码是我目前所拥有的,它将遍历文件夹,但是当它到达正确的日期时找不到它。我尝试了几种变体,但当我尝试 运行 时,Access 总是崩溃。理想情况下,我需要 Access 来查找文件的最新日期并将其导入,例如星期一进来并获取 Friday/Saturday 的文件,或者在一周内获取前一天的文件。任何帮助将不胜感激。
Private Sub Button1_Click()
Dim strToday As String
Dim strFilePath as String
Dim strFile as String
strToday = Format(Date, "m-dd-yyyy")
strFilePath = "C:\Users\cole.stratton\Documents\Procurement\FTTQ 'Note:FTTQ is the beginning of the file name
strFile = Dir(strFilePath, "*.xlsx")
Do While strFile <> ""
If Right(strFile,14) = strToday & ".xlsx" Then
DoCmd.TransferSpreadsheet, acImport, "tblTest",strFile, True
End If
strFile = Dir 'Note: I do not understand the point of this line or what it does or supposed to do.
Loop
End Sub
***我假设您在实际代码的 strFilePath 行中有结束符 "。****
这一行看起来像问题...
strFile = Dir(strFilePath, "*.xlsx")
此页面将向您展示使用目录的正确语法...http://www.techonthenet.com/excel/formulas/dir.php
strFile = Dir(strFilePath & "*.xlsx")
<-- 您将文件扩展名放在属性应该去的地方。
但是,您还需要更改日期。如果文件有昨天的日期,而不是今天的...strToday = Format(Date-1, "m-dd-yyyy")
这一行...
strFile = Dir
将您的字符串设置为下一个符合搜索条件的文件名。
要查找最新的现有文件,我会像这样更改循环:
Dim searchDate As Date
Dim strDate As String
Dim strFilePath As String
Dim strFile As String
Dim i As Long
' Search backwards from today for a file with the date name
For i = 0 To -7 Step -1
searchDate = DateAdd("d", i, Date)
strDate = Format(searchDate, "m-dd-yyyy")
strFilePath = "C:\Users\cole.stratton\Documents\Procurement\FTTQ " & strDate & ".xlsx"
Debug.Print "Looking for: " & strFilePath
' Check if file exists
strFile = Dir(strFilePath)
If strFile <> "" Then
' Note that Dir() only returns the file name, so use strFilePath
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tblTest", strFilePath, True
' file found, exit loop
Exit For
End If
Next i
如果有人在某个地方提出这个问题而我忽略了它,请让我提前道歉。我在这上面花了很多天,但无法达到 运行 100%。
我正在尝试将每天早上通过电子邮件发送的 excel 文件导入到访问数据库中。该文件的日期部分每天都在变化。命名每天都遵循相同的模式 "FTTQ m-dd-yyyy"。文件名中显示的日期是前一个工作日,例如。 8 月 25 日收到 8 月 24 日 FTTQ 的电子邮件。下面的代码是我目前所拥有的,它将遍历文件夹,但是当它到达正确的日期时找不到它。我尝试了几种变体,但当我尝试 运行 时,Access 总是崩溃。理想情况下,我需要 Access 来查找文件的最新日期并将其导入,例如星期一进来并获取 Friday/Saturday 的文件,或者在一周内获取前一天的文件。任何帮助将不胜感激。
Private Sub Button1_Click()
Dim strToday As String
Dim strFilePath as String
Dim strFile as String
strToday = Format(Date, "m-dd-yyyy")
strFilePath = "C:\Users\cole.stratton\Documents\Procurement\FTTQ 'Note:FTTQ is the beginning of the file name
strFile = Dir(strFilePath, "*.xlsx")
Do While strFile <> ""
If Right(strFile,14) = strToday & ".xlsx" Then
DoCmd.TransferSpreadsheet, acImport, "tblTest",strFile, True
End If
strFile = Dir 'Note: I do not understand the point of this line or what it does or supposed to do.
Loop
End Sub
***我假设您在实际代码的 strFilePath 行中有结束符 "。****
这一行看起来像问题...
strFile = Dir(strFilePath, "*.xlsx")
此页面将向您展示使用目录的正确语法...http://www.techonthenet.com/excel/formulas/dir.php
strFile = Dir(strFilePath & "*.xlsx")
<-- 您将文件扩展名放在属性应该去的地方。
但是,您还需要更改日期。如果文件有昨天的日期,而不是今天的...strToday = Format(Date-1, "m-dd-yyyy")
这一行...
strFile = Dir
将您的字符串设置为下一个符合搜索条件的文件名。
要查找最新的现有文件,我会像这样更改循环:
Dim searchDate As Date
Dim strDate As String
Dim strFilePath As String
Dim strFile As String
Dim i As Long
' Search backwards from today for a file with the date name
For i = 0 To -7 Step -1
searchDate = DateAdd("d", i, Date)
strDate = Format(searchDate, "m-dd-yyyy")
strFilePath = "C:\Users\cole.stratton\Documents\Procurement\FTTQ " & strDate & ".xlsx"
Debug.Print "Looking for: " & strFilePath
' Check if file exists
strFile = Dir(strFilePath)
If strFile <> "" Then
' Note that Dir() only returns the file name, so use strFilePath
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "tblTest", strFilePath, True
' file found, exit loop
Exit For
End If
Next i