在MSOffice Access 2010中移动文件

时间:2013-07-22 14:03:35

标签: vba ms-access access-vba

我正在尝试在Access中的某个表单上创建一个按钮,该按钮会将文件从一个文件夹移动到另一个文件夹。项目的文件路径存储在数据库中。我目前的方法是使用VB并显示在这里。

Private Sub Command21_Click()
    Dim d As Database
    Dim r As Recordset
    Dim path As Field
    Dim fromPath As String
    Dim toPath As String
    Set d = CurrentDb()
    Set r = d.OpenRecordset("Documents")
    Set path = r.Fields("Action Items Location")
    While Not r.EOF
        fromPath = path
        Set toPath = My.Computer.FileSystem.GetParentPath(fromPath) 'Error line
        toPath = toPath & "\to folder"
        My.Computer.FileSystem.MoveFile fromPath, toPath
    Wend

End Sub

我一直收到一条错误,指出标记为错误行的行所需的对象。我该如何解决这个错误,或者我是否以正确的方式解决这个问题?

2 个答案:

答案 0 :(得分:1)

感谢您的回复,虽然经过一些研究,以及@Basdwarf的建议,我能够找到解决方案。这是完成的代码

Private Sub Command21_Click()
    Dim d As Database
    Dim r As Recordset
    Dim path As Field

    Dim fromPath As String
    Dim toPath As String
    Dim fileName As String

    Dim filesystem As Object

    Set filesystem = CreateObject("Scripting.FilesystemObject")
    Set d = CurrentDb()
    Set r = d.OpenRecordset("Documents")
    Set path = r.Fields("Action Items Location")

    fromPath = path
    fileName = filesystem.GetFileName(path)
    toPath = filesystem.GetParentFolderName(filesystem.GetParentFolderName(fromPath)) & "\to folder" & "\" & fileName
    MsgBox (fromPath)
    MsgBox (toPath)
    FileCopy fromPath, toPath
    Kill fromPath
End Sub

答案 1 :(得分:0)

GetParentPath不是Access中VBA.Filesystem类中的可用方法。 进入代码,视图,对象浏览器,搜索文件系统以获取可用的方法。

您可以使用GetFileInfo查找文件目录。