2016-10-24 57 views
0

我只有今天瞭解了FSO,所以我想這樣的進口統計小時(一個新的文件出現在文件夾中每小時),以實現它在我的數據庫系統對象方法的問題

當我運行的代碼,它找到的文件,但不導入數據,並拋出一個運行時錯誤3011

我的模塊代碼:

Function getFilename(path As String) As String 
Dim objFSO As Object 
Dim objFolder As Object 
Dim objFile As Object 
Dim dateModified As Date 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFolder = objFSO.GetFolder(path) 

For Each objFile In objFolder.Files 
Debug.Print objFile.Name 
If InStr(objFile.Name, "hourly performance") > 0 And Not Left(objFile.Name, 1) = "~" Then 
    If objFile.datelastmodified > dateModified Then 
    dateModified = objFile.datelastmodified 
    getFilename = objFile.Name 
    End If 

End If 

Next objFile 

End Function 

我的VBA代碼則是:

Dim strFilename As String 

strFilename = getFilename("C:\stats folder hourly\") 

' Import Weekly stats 
      DoCmd.TransferSpreadsheet transfertype:=acImport, SpreadsheetType:=10, _ 
      tablename:="Weekly", FileName:=strFilename, _ 
      Hasfieldnames:=True, Range:="AgentActivity CSV!" 

錯誤:

Microsoft access database engine could not find the object 'FILENAME' Make sure the object exists and that you spell the name and the path name correctly

幫助

+0

您的getFileName'()'函數將返回一個空字符串'「」'或_simple_文件名(不帶路徑)。對於第一個問題,你應該添加一個檢查'如果strFilename <>「」然後DoCmd.TransferSpreadsheet ...「至於第二個問題,DoCmd.TransferSpreadsheet需要'strFilename'作爲_pure_名字還是需要完整路徑?最後是否需要打開該文件? – user3598756

回答

0

我終於解決了這個問題

DoCmd.TransferSpreadsheet transfertype:=acImport, SpreadsheetType:=10, _ 
    tablename:="Hourly", FileName:="FILPATH" & hourly & "", _ 
    Hasfieldnames:=True, Range:="AgentActivity CSV!" 
0

嘗試改變

getFilename = objFile.Name 

getFilename = objFSO.GetAbsolutePathName(objFile) 

至極會給你像C絕對路徑:\文件夾\ file.ext