I use this code in my VBA applications to test if a date is Null:
Is there a similar technique in VB.Net?Code:Dim varItem As Variant Dim dtmMaxDate as Date varItem = DMax("RecDate", "Asset Prices") If Not IsNull(varItem) Then dtmMaxDate = CDate(varItem) End If
For example, this code raises an error when MaxOfRecDate is Null:
My workaround has been to check if the count is 0 ("SELECT Count([Asset Prices].RecDate) AS CountOfRecDate ...") and then if it is not to do the second query to get the date value:Code:Dim dtmDate As Date strSQL = "SELECT Max([Asset Prices].RecDate) AS MaxOfRecDate " & _ "FROM [Asset Prices] " & _ "WHERE ([Asset Prices].AssetID=" & lngAssetID & ")" cmd.CommandText = strSQL dtmDate = Convert.ToDateTime(cmd.ExecuteScalar())
Is there a way to avoid having to use 2 queries to check for a Null date?Code:strSQL = "SELECT Count([Asset Prices].RecDate) AS CountOfRecDate " & _ "FROM [Asset Prices] " & _ "WHERE ([Asset Prices].AssetID=" & lngAssetID & ")" cmd.CommandText = strSQL intResult = Convert.ToInt32(cmd.ExecuteScalar) If intResult = 0 Then dtmDate = #12:00:00 AM# Else strSQL = "SELECT Max([Asset Prices].RecDate) AS MaxOfRecDate " & _ "FROM [Asset Prices] " & _ "WHERE ([Asset Prices].AssetID=" & lngAssetID & ")" cmd.CommandText = strSQL dtmDate = Convert.ToDateTime(cmd.ExecuteScalar()) End If




Reply With Quote
