Question : Null value problem with VBA and Access.

I have the following sub and function. I get an error when the folder field in the database is null. The error says "Invalid use of null" and
CurrentDb.Execute ("update modHist set LastFileDate = #" & DateAdd("d", -1 * NewestFileInFolder(rs("folder")), Date) & "# where fundName = '" & rs("fundname") & "'")
is highlighted.

How can I make this work with Null values?

Public Sub ahh()

Dim rs As Recordset

Set rs = CurrentDb.OpenRecordset("select fundname, folder from FundInfo", dbOpenSnapshot)
Do While Not rs.EOF

'If NewestFileInFolder(rs("folder")) Then

CurrentDb.Execute ("update modHist set LastFileDate = #" & DateAdd("d", -1 * NewestFileInFolder(rs("folder")), Date) & "# where fundName = '" & rs("fundname") & "'")

'End If

rs.MoveNext

Loop
End Sub

Public Function DateNewestFileInFolder(strFolderPath As String) As Integer
   If Dir(strFolderPath) = "" Then
   Exit Function
   
   
   
    Dim objFSO As FileSystemObject, objFolder As Object, objFile As File, intTemp As Date, bolFirstPass As Boolean
    Set objFSO = CreateObject("Scripting.FileSystemobject")
    Set objFolder = objFSO.GetFolder(strFolderPath)
    bolFirstPass = True
    For Each objFile In objFolder.Files
        intTemp = objFile.DateCreated
        If bolFirstPass Then
           DateNewestFileInFolder = intTemp
            bolFirstPass = False
        Else
            If intTemp < DateNewestFileInFolder Then DateNewestFileInFolder = intTemp
           
        End If
    Next
    Set objFile = Nothing
    Set objFolder = Nothing
    Set objFSO = Nothing
End Function

Answer : Null value problem with VBA and Access.

Assuming you simply want to skip rows where that column is null, change:

Set rs = CurrentDb.OpenRecordset("select fundname, folder from FundInfo", dbOpenSnapshot)

to:

Set rs = CurrentDb.OpenRecordset("select fundname, folder from FundInfo where folder is not null", dbOpenSnapshot)
Random Solutions  
 
programming4us programming4us