vba 在 MSOffice Access 2010 中移动文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17789599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:22:13  来源:igfitidea点击:

Moving Files in MSOffice Access 2010

vbams-accessaccess-vba

提问by madattarp

I am attempting to create a button on one of my forms in Access that will move a file from one folder to another. The filepath of the item is stored in the database. My current approach is using VB and is displayed here.

我正在尝试在 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

I keep getting an error saying object required on the line marked Error line. How do I fix this error, or am I even going about it the correct way?

我不断收到错误消息,指出在标记为错误行的行上需要对象。我该如何解决这个错误,或者我是否以正确的方式解决这个问题?

采纳答案by madattarp

Thanks for the replies, though after a bit more research, and the suggestion of @Basdwarf, I was able to find a solution. Here's the finished code

感谢您的回复,但经过更多研究和@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

回答by basdwarf

GetParentPath is not an available method in the VBA.Filesystem class in Access. Go into Code, View, Object Browser, search Filesystem for available methods.

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

You can use GetFileInfo to find the files directory.

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