Results 1 to 7 of 7

Thread: [RESOLVED] Checking for a Null date

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    528

    Resolved [RESOLVED] Checking for a Null date

    I use this code in my VBA applications to test if a date is Null:

    Code:
    Dim varItem As Variant
    Dim dtmMaxDate as Date
    varItem = DMax("RecDate", "Asset Prices")
    If Not IsNull(varItem) Then
        dtmMaxDate = CDate(varItem)
    End If
    Is there a similar technique in VB.Net?

    For example, this code raises an error when MaxOfRecDate is Null:

    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())
    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:
    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
    Is there a way to avoid having to use 2 queries to check for a Null date?
    Last edited by Mark@SF; Jun 28th, 2017 at 03:15 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width