导入Access DB后移动文件

时间:2015-01-27 13:20:33

标签: csv access-vba

我在Access中开发VB宏以将CSV文件导入Access数据库,我想在处理完成后移动这些文件。

Public Sub RepertoireList()

Dim FolderName As String
Dim FolderNameMove As String
Dim FileList As String
Dim FileListMove As String
Dim FileToBeLoaded As String

tblName = "tbl_Test" 'This is to import all files into one table.
FolderName = "c:\CSV\"
FolderNameMove = "c:\CSV\Old"
FileList = Dir(FolderName & "\*.csv")
FileListMove = Dir(FolderNameMove & "\*.csv")

While (Len(Trim$(FileList)) > 0)        

    If (Len(Trim$(FileList))) > 0 Then
        FileToBeLoaded = FolderName & FileList
        DoCmd.TransferText acImportDelim, "Test2 Import Specification", tblName, FileToBeLoaded, False

    End If
    FileList = Dir

    'Move files after import
    Sub Deplacer()
         Dim FSO As Object
         Set FSO = CreateObject("scripting.filesystemobject")
         FSO.MoveFile Source:=FolderName, Destination:=FolderNameMove
    End Sub       

Wend    

End Sub

导入工作正常但移动文件没有正确完成。

2 个答案:

答案 0 :(得分:0)

我修改了脚本:

Public Sub RepertoireList()

Dim FolderName As String
Dim FileList As String
Dim FileToBeLoaded As String

tblName = "CV_Test" 'This is to import all files into one table.
FolderName = "c:\CSV\"
FolderNameMove = "c:\CSV\Old"
FileList = Dir(FolderName & "\*.csv")

While (Len(Trim$(FileList)) > 0)
    If (Len(Trim$(FileList))) > 0 Then
        FileToBeLoaded = FolderName & FileList
        DoCmd.TransferText acImportDelim, "Test2 Import Specification", tblName, FileToBeLoaded, False
    End If
    FileList = Dir
Wend

'Move files after import
        Set FSO = CreateObject("scripting.filesystemobject")
        FSO.MoveFile Source:=FolderName, Destination:=FolderNameMove
End Sub

但我在调试模式下出现错误:"运行时错误' 53'文件未找到" 我不明白,因为导入时,文件是在Access DB中导入的。

答案 1 :(得分:0)

Dim FolderName As String
Dim FileList As String
Dim FileToBeLoaded As String

tblName = "CV_Test" 'This is to import all files into one table.
FolderName = "c:\CSV\"
FolderNameMove = "c:\CSV\Old\"
FileList = Dir(FolderName & "\*.csv")

While (Len(Trim$(FileList)) > 0)
    If (Len(Trim$(FileList))) > 0 Then
        FileToBeLoaded = FolderName & FileList
        DoCmd.TransferText acImportDelim, "Test2 Import Specification", tblName, FileToBeLoaded, False
    End If
    FileList = Dir
Wend

'Move files after import
FileList = Dir(FolderName & "\*.csv")
While (Len(Trim$(FileList)) > 0)
    If (Len(Trim$(FileList))) > 0 Then
        Set FSO = CreateObject("scripting.filesystemobject")
        FSO.MoveFile Source:=FolderName & FileList, Destination:=FolderNameMove
    End If
    FileList = Dir

Wend