Question : How can I make this function to work with null values? In VBA and MS Access

I use this following code to check for the most recently created folder in specific path for each entry in MS Access. However when there is no folder set for and entry it gives the null value error.

thanks

courtesy to bluedevilfan
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
Public Function NewestFileInFolder(strFolderPath As String) As Integer
    Dim objFSO As FileSystemObject, objFolder As Object, objFile As File, intTemp As Integer, bolFirstPass As Boolean
    Set objFSO = CreateObject("Scripting.FileSystemobject")
    Set objFolder = objFSO.GetFolder(strFolderPath)
    bolFirstPass = True
    
    
    For Each objFile In objFolder.Files
        intTemp = DateDiff("d", objFile.DateCreated, Date)
        If bolFirstPass Then
            NewestFileInFolder = intTemp
            bolFirstPass = False
        Else
            If intTemp < NewestFileInFolder Then NewestFileInFolder = intTemp
        End If
    Next
    


    Set objFile = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing



End Function

Answer : How can I make this function to work with null values? In VBA and MS Access

Try this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Public Function NewestFileInFolder(strFolderPath As String) As Integer
    If Dir(strFolderPath) = "" Then
        Exit Function
    End If
    Dim objFSO As FileSystemObject, objFolder As Object, objFile As File, intTemp As Integer, bolFirstPass As Boolean
    Set objFSO = New Scripting.FileSystemObject

    Set objFolder = objFSO.GetFolder(strFolderPath)
    bolFirstPass = True
    
    For Each objFile In objFolder.Files
        intTemp = DateDiff("d", objFile.DateCreated, Date)
        If bolFirstPass Then
            NewestFileInFolder = intTemp
            bolFirstPass = False
        Else
            If intTemp < NewestFileInFolder Then NewestFileInFolder = intTemp
        End If
    Next
    
    Set objFile = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing
End Function
Random Solutions  
 
programming4us programming4us