【问题标题】:Access VBA "Path not found" when using MkDir to create folder on desktop使用 MkDir 在桌面上创建文件夹时访问 VBA“找不到路径”
【发布时间】:2021-08-18 18:58:07
【问题描述】:

我正在尝试从 MS Access 中的表单创建文件夹。我需要能够为每个表单条目创建一个新文件夹。我是一名新手 VBA 程序员,但通过在线搜索和测试技术学到了很多东西。代码如下:

Private Sub Create_File_Folder_Click()


Engine.SetFocus
    If Dir("C:\Users\ndemos\Documents\Test\" & TimeDateTeam.Value, vbDirectory) = "" Then
    MkDir ("C:\Users\ndemos\Documents\Test\" & TimeDateTeam.Value)

Const strParent = "C:\Users\ndemos\Desktop\Test\"
    Dim strTimeDateTeam As String
    Dim strFolder As String
    Dim fso As Object
strStudentID = Me.TimeDateTeam
strFolder = strParent & strStudentID
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(strFolder) Then
MsgBox "Specified folder already exists.", vbInformation, "File Folder"
    Exit Sub
    
Else

fso.CreateFolder strFolder
End If
Shell "explorer.exe " & strFolder, vbNormalFocus

End If    
End Sub

当我尝试单击表单中的按钮以创建文件夹时,我遇到了运行时错误 76“找不到路径”。我已经确定该路径确实存在,并且尝试了多个其他方向的文件夹,但都失败了(例如 \Documents\ 等等)。

任何有关如何创建文件夹的帮助将不胜感激。

【问题讨论】:

  • 您是否安装了OneDrive?右键单击“文档”文件夹 -> 属性,然后查看 Location 显示的内容。是像“C:\Users\YourUser”还是“C:\Users\YourUser\OneDrive”?

标签: vba ms-access runtime-error mkdir


【解决方案1】:

首先,“\Test”必须存在。然后这将起作用:

Path = Environ("UserProfile") & "\Documents\Test\" & Me!TimeDateTeam.Value
If Dir(Path, vbDirectory) = "" Then
    MkDir Path
End If

【讨论】:

  • 是的,它在任何情况下都可以使用,但是如果您安装了 OneDrive,从此刻开始,您将拥有两个“文档”文件夹。您的用户配置文件中的一个(仅保留“测试”文件夹)和系统中的一个路径为“C:\Users\YourUser\OneDrive\Documents”...
  • @FaneDuru:我认为,这取决于您如何(或是否)将 Documents 文件夹同步到 OneDrive。
  • 是的,就是这样。默认情况下,如果您不更改它,“文档”文件夹将包含在同步列表中。至少,这发生在我的案例中。当需要设置这样一个文件夹的路径时,我使用一个函数(“OneDriveExists”)。如果已安装,Environ$("ONEDRIVE") 将返回其路径。
  • 谢谢! Environ$("ONEDRIVE") 成功了。如果我想在文件夹名称已经存在的情况下添加一个 MsgBox,我该怎么做?我尝试了几种不同的技术,但大多返回错误。我认为应该有一个简单的“If - else”解决方案,但我不能完全确定它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
  • 1970-01-01
  • 2023-02-15
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 2018-08-29
相关资源
最近更新 更多