Search code examples
ms-accessvbaisnullfiltered-lookup

DLookup In Access 2010 using IsNull and multiple criteria


Essentially I am trying to find if a user is currently logged in before making a new row with TimeIn, but with the code as it is written, a user can check in multiple times without checking out (TLog_TImeOut field is null). My code is listed below.

If Not IsNull(DLookup("[TLog_TimeIn]", "TIMELOG", "IsNull(TLog_TImeOut)= True And NetID =  '[TempVars]![CurrentID]'")) Then
MsgBox ("Please Check out!")
DoCmd.Close acForm, "CHECKIN", acSaveNo
Exit Sub
Else
   NetID = [TempVars]![CurrentID]
   TLog_TimeIn = Now()
   MsgBox ("Thanks for checking in!")
   DoCmd.Close acForm, "CHECKIN", acSaveYes
Exit Sub
End If

Solution

  • Your DLOOKUP should be more like this:

    If Not IsNull(DLookup("[TLog_TimeIn]", "TIMELOG", "TLog_TimeOut Is Null And NetID = '" & Forms![TempVars]![CurrentID] & "'")) Then
    

    Specifically your condition -

    IsNull(TLog_TImeOut)= True And NetID =  '[TempVars]![CurrentID]'
    

    Should be treated like the WHERE condition in a SQL statement.

    First, if possible, you want to avoid Access functions for speed reasons (not a big deal in this particular case) so using Is Null is better than IsNull(XXX)=True but especially this part:

    NetID =  '[TempVars]![CurrentID]'
    

    This is going to literally compare NetID to the string '[TempVar]![CurrentID]' so what you want is to build this string like this:

    NetID = '" & Forms![TempVars]![CurrentID] & "'"
    

    (Assuming [TempVars] is a hidden form that you use to save variable values) which if CurrentID is BOB on that form will yield:

    NetID = 'BOB'
    

    Which is what you want.