• vba understanding (97nl)

    Author
    Topic
    #406417

    Hello all,

    Could someone tell me why example 1 does not work and example 2 will work:

    example 1
    ———————————–
    If numPrio = 0 Then
    If datD1 = Null Then
    datDeadline = datHD
    Else
    datDeadline = datD1
    End If
    Else
    datDeadline = datD2
    End If
    ———————————–

    example 2
    ———————————–
    If numPrio = 0 And datD1 = Null Then datDeadline = datHD
    If numPrio = 0 And datD1 = Null Then datDeadline = datD1
    If numPrio 0 Then datDeadline = datD2
    ———————————–

    They seem the same to me, but they word different
    Thanks in advance.

    Viewing 1 reply thread
    Author
    Replies
    • #842647

      Perhaps you didn’t copy Example 2 correctly: it contains “If numPrio = 0 And datD1 = Null Then” twice.

      In fact, both code samples are incorrect. You cannot compare a value to Null by testing “If … = Null Then”, since Null is not really a value, it indicates the lack of a value. Use the IsNull function instead:

      If NumPrio = 0 Then
      If IsNull(datD1) Then
      datDeadline = datHD
      Else
      datDeadline = datD1
      End If
      Else
      datHeadline = datD2
      End If

      Even shorter is the use of the Access-specific Nz function:

      If NumPrio = 0 Then
      datDeadline = Nz(datD1, datHD)
      Else
      datHeadline = datD2
      End If

      Nz returrns the first argument, unless that is Null, then it returns the second argument.

      • #842655

        Great Hans,

        Thanks, I understand the problem now

      • #842656

        Great Hans,

        Thanks, I understand the problem now

    • #842648

      Perhaps you didn’t copy Example 2 correctly: it contains “If numPrio = 0 And datD1 = Null Then” twice.

      In fact, both code samples are incorrect. You cannot compare a value to Null by testing “If … = Null Then”, since Null is not really a value, it indicates the lack of a value. Use the IsNull function instead:

      If NumPrio = 0 Then
      If IsNull(datD1) Then
      datDeadline = datHD
      Else
      datDeadline = datD1
      End If
      Else
      datHeadline = datD2
      End If

      Even shorter is the use of the Access-specific Nz function:

      If NumPrio = 0 Then
      datDeadline = Nz(datD1, datHD)
      Else
      datHeadline = datD2
      End If

      Nz returrns the first argument, unless that is Null, then it returns the second argument.

    Viewing 1 reply thread
    Reply To: vba understanding (97nl)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: