2013-07-22 24 views
2

我正嘗試在Access中的一個窗體上創建一個按鈕,將文件從一個文件夾移動到另一個文件夾。該項目的文件路徑存儲在數據庫中。我目前的做法是使用VB並顯示在這裏。在MSOffice Access 2010中移動文件

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 

我不斷收到一個錯誤,指出在標記爲錯誤行的行上需要的對象。我該如何解決這個錯誤,或者我甚至以正確的方式去解決這個問題?

+1

嘗試沒有「設置」或看看可能的錯誤異常這裏http://msdn.microsoft.com/en-us/library/b573ycc1(v=vs.90).aspx –

+0

原本沒有「設置」,它仍然有相同的錯誤。實際上,我在審查後添加了這個設置:http://msdn.microsoft.com/en-us/library/office/gg251554.aspx – madattarp

+1

您使用的是VBA還是VB?從這個鏈接看來,'My.Computer.FileSystem.GetParentPath()'是一個VB函數,可能在VBA中找不到。 – Zaider

回答

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 
0

GetParentPath不是Access中VBA.Filesystem類中的可用方法。 進入代碼,視圖,對象瀏覽器,搜索文件系統查找可用的方法。

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

+0

感謝您對對象瀏覽器的建議,不知道那裏。但是,我不能GetFileInfo函數。 – madattarp