Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

VBA and Nulls?

Back to Forum: Tech Off
  • User profile image
    W3bbo

    Consider this bit of VBA:


    Private Sub cmbParticipant_Change()
    If cmbRounds.Column(0) <> Null Then
    ParticipantID = CInt(cmbParticipant.Column(0)) ' Column zero is the Identity col
    End If
    End Sub

    When I comment out the "if" and "end if" bits (so the ParticipantID is always set) I sometimes get errors about setting null values.

    When I run the program and test the value of cmbRounds.Column(0) in the Immediate window, it returns an integer value "4", but when I step though the program jumps straight to the "End If", as if cmbRounds.Column(0) was equal to "Null", which it isn't.

    When I change "Null" to "Nothing" I get another error about types.

  • User profile image
    LeighS

    You should be able to use the IsNull() function

     

    Private Sub cmbParticipant_Change()
        If not IsNull(cmbRounds.Column(0)) Then
            ParticipantID = CInt(cmbParticipant.Column(0))   ' Column zero is the Identity col
        End If
    End Sub

  • User profile image
    Maurits

    I've used VarType(...) = vbNull in similar situations.

  • User profile image
    Red5

    W3bbo wrote:
    Consider this bit of VBA:


    Private Sub cmbParticipant_Change()
    If cmbRounds.Column(0) <> Null Then
    ParticipantID = CInt(cmbParticipant.Column(0)) ' Column zero is the Identity col
    End If
    End Sub



    This should work too:

    Private Sub cmbParticipant_Change()
        If Not cmbRounds.Column(0) Is System.DBull.Value Then
            ParticipantID = CInt(cmbParticipant.Column(0))   ' Column zero is the Identity col
        End If
    End Sub

  • User profile image
    Sven Groot

    Red5 wrote:
    
    W3bbo wrote: Consider this bit of VBA:


    Private Sub cmbParticipant_Change()
    If cmbRounds.Column(0) <> Null Then
    ParticipantID = CInt(cmbParticipant.Column(0)) ' Column zero is the Identity col
    End If
    End Sub



    This should work too:

    Private Sub cmbParticipant_Change()
        If Not cmbRounds.Column(0) Is System.DBull.Value Then
            ParticipantID = CInt(cmbParticipant.Column(0))   ' Column zero is the Identity col
        End If
    End Sub


    Sorry, he's using VBA, not VB.NET.

  • User profile image
    Red5

    Sven Groot wrote:
    Sorry, he's using VBA, not VB.NET.


    Duh.  Sorry, hastily entered response.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.