PDA

View Full Version : Exit function after error



Hammie
08-17-2010, 10:14 AM
Hello,

I have a sub for making folders. From this sub I call a function wich creates the folder.

This is the code for the Sub and the Function:


Private Sub CreateFolders()
'Get the Root folder from the table tblSystemSettings
gsDataRootFolder = CheckFolder(rstSystemSettings("DataRootFolder"))

'Make a year folder
sYearFolder = CheckFolder(gsDataRootFolder & "2010")
End Sub
Public Function CheckFolder(sFolder As String)
'========================================================
'
'This function checks if a folder already exists and if
'neccasary it creates the folder
'
'========================================================
'Create FileSystemObject
Dim fso As Scripting.FileSystemObject


Set fso = CreateObject("Scripting.FileSystemObject")

'Check if the string sFolder ends with a \
If Right(sFolder, 1) <> "\" Then
sFolder = sFolder & "\"
End If

'Check if sFolder exists
If fso.FolderExists(sFolder) = False Then
'Create sFolder
fso.CreateFolder (sFolder)

'Check if the creation of sFolder was succeeded
If fso.FolderExists(sFolder) = False Then
'The creation of sFolder was not successful
MsgBox GetMessage(18, gsLanguage, "alert"), vbCritical, GetMessage(18, gsLanguage, "titel")
Exit Function
End If
End If

CheckFolder = sFolder
Set fso = Nothing
End Function


What I want is when the creation of the folder wasn't successful it exists the sub immediately.
Edited 20-Aug-10 by geekgirlau. Reason: add vba tags

Imdabaum
08-17-2010, 11:12 AM
Doesn't it already exit the function if the folder doesn't exist after attempting to create it?

hansup
08-19-2010, 06:06 PM
Does this do what you want?

Private Sub CreateFolders()
'Get the Root folder from the table tblSystemSettings
gsDataRootFolder = CheckFolder(rstSystemSettings("DataRootFolder"))
If Len(Dir(gsDataRootFolder)) > 0 Then
'Make a year folder
sYearFolder = CheckFolder(gsDataRootFolder & "2010")
End If
End Sub