locked
Solve the message "invalid use of null" RRS feed

  • Question

  • Hai one and all,

    Kindly guide me to solve the following issue

    there is a form for updating students attendance. in that form there is column for Course. based on this field there written "dlookup" function for another field that is total working days in that month.

     after entering last student details if i go to next, immediately i am getting "Run time error 94, Invalid use of null is coming and the vba window is getting opened. and there is a yellow mark in the vba code "txtSep = Me.Course.Value"

    the code is 

    Dim wdSep As Integer
    Dim txtSep As String
    txtSep = Me.Course.Value
    wdSep = DLookup("SepWDC", "CoursesSCPM", "ID = " & txtSep)
    Me.SepWD.Value = wdSep

    Dim wdOct As Integer
    Dim txtOct As String
    txtOct = Me.Course.Value
    wdOct = DLookup("OctWDC", "CoursesSCPM", "ID = " & txtOct)
    Me.OctWD.Value = wdOct

    Kindly help to solve this issue.

    Wednesday, June 27, 2018 5:48 PM

All replies

  • Hi AspinR,

    The reason for this error is that wdOct is declared as an Integer. If no record satisfies criteria or if domain contains no records, the DLookup function returns a Null. An Integer cannot be Null, but a Variant does.

    You have to declare wdOct as an Variant:

    Dim wdOct As Variant

    A Variant can contain a Null Value. After you set the value for wdOct by DLookUp, you have to check wdOct, for example:

        If IsNull(wdOct) Then
            Exit Sub
        Else
            ' Instructions..
        End If


    Wednesday, June 27, 2018 6:22 PM
  • Hi AspinR,

    If the error is happening on the line:

    txtSep = Me.Course.Value

    It means the control on your form called Course is empty or Null.

    To avoid this error, try using the Nz() function. For example:

    txtSep = Nz(Me.Course.Value,"")

    Hope it helps...

    Thursday, June 28, 2018 1:31 AM