MS Access创建多级子文件夹

时间:2015-04-20 16:40:10

标签: vba ms-access

如何在VBA for MS Access中以编程方式创建多个级别的子文件夹?我知道MKDir只允许我创建一个级别,但我想创建2个级别。第一级文件夹基于发货的年份,然后是子级文件夹,即货件号。我们的想法是检查并查看文件夹是否存在,如果不存在则打开它们。

这是我到目前为止所做的:

Private Sub Command173_Click()
 Const strParent = "S:\shipments\"
 Dim strYearEntered As String
 Dim strEntryNumber As String
 Dim strFolder As String
 Dim fso As Object
 strYearEntered = Me.YearEntered
 strEntryNumber = Me.EntryNum
 strFolder = strParent & strYearEntered & "\" & strEntryNumber
 Set fso = CreateObject("Scripting.FileSystemObject")
 If fso.FOLDEREXISTS(strFolder) = False Then
     fso.CreateFolder strFolder
 End If
 Shell "explorer.exe " & strFolder, vbNormalFocus

End Sub

使用此代码会在" fso.CreateFolder strFolder"中出现错误。线。这个问题只发生在我放置" \"在strFolder行中,没有" \"它只会通过将YearEntered和EntryNum值填入一起来创建一个文件夹。任何人都可以协助解决这个问题吗?

感谢。

1 个答案:

答案 0 :(得分:2)

Private Sub Command173_Click()
 Const strParent = "S:\shipments\"
 Dim strYearEntered As String
 Dim strEntryNumber As String
 Dim strFolder As String

 strYearEntered = Me.YearEntered
 strEntryNumber = Me.EntryNum

 strFolder = strParent & strYearEntered
 If Dir(strFolder, vbDirectory) = "" then MkDir strFolder 

 strFolder = strFolder & "\" & strEntryNumber 
 If Dir(strFolder, vbDirectory) = "" then MkDir strFolder 

 Shell "explorer.exe " & strFolder, vbNormalFocus

End Sub