Results 1 to 6 of 6
  1. #1
    Hasher is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    May 2013
    Posts
    13

    Dlookup with conditional problem

    Gday

    I am trying to match if a user has already started a record using the date and username. I have written the following code but I keep getting a error 94 about null. I know its an issue involving the date part.

    Any help would be great with why this isn't working.


    Code:
    DLookup("TimeSheetID", "TimeSheet", "EmployeeID=" & Nz(Me.ListEmploy.Value) & " AND TimeSheetDate=#" & Date2 & "#")))
    The full code

    Code:
     
       Dim Date2 As Date
    
    
        Date2 = Date
    If (Not IsNull(DLookup("TimeSheetID", "TimeSheet", "EmployeeID=" & Nz(Me.ListEmploy.Value) & " AND TimeSheetDate=#" & Date2 & "#"))) Then 
               lngEmployeeID = DLookup("TimeSheetID", "TimeSheet", "EmployeeID=" & Nz(Me.ListEmploy.Value) & " AND TimeSheetDate=#" & Date2 & "#")
      DoCmd.OpenForm "frmTimeSheetMain", , , "TimeSheetID=" & Nz(lngEmployeeID), , , "NoTimeSheetID"


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    You cant run it if date is null, so prevent it:

    Code:
    if IsNull(Date2) then 
       msgbox " Date missing"
    else
        DLookup("TimeSheetID", "TimeSheet", "EmployeeID=" & Nz(Me.ListEmploy.Value) & " AND TimeSheetDate=#" & Date2 & "#")))
    ...

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    There is no problem with Date2, because you specify Date2 = Date.

    Can you be sure that this statement:

    lngEmployeeID = DLookup("TimeSheetID", "TimeSheet", "EmployeeID=" & Nz(Me.ListEmploy.Value) & " AND TimeSheetDate=#" & Date2 & "#")

    does not return a Null? A Null will cause an error if lngEmployeeID is not a Variant type.

    Try this:

    lngEmployeeID = nz(DLookup("TimeSheetID", "TimeSheet", "EmployeeID=" & Nz(Me.ListEmploy.Value) & " AND TimeSheetDate=#" & Date2 & "#"), -1)
    msgbox "Employee ID = " & lngEmployeeID

    to verify that you are gerring the right data.

    John

  4. #4
    Hasher is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    May 2013
    Posts
    13
    Thanks John_G for the reply.

    I am still having the problem. I tried your suggestion and it returned "-1"

    The problem is how I am doing the date as I have tried it without the conditional AND and date and it finds the employee just fine.

    Any other suggestions or should I try another approach ?

    Thanks
    Paul

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Why bother with the Date2 variable?

    Dim lngEmployeeID As Long
    lngEmployeeID = Nz(DLookup("TimeSheetID", "TimeSheet", "EmployeeID=" & Nz(Me.ListEmploy, 0) & " AND TimeSheetDate=Date()"), 0)
    If lngEmployeeID > 0 Then
    DoCmd.OpenForm "frmTimeSheetMain", , , "TimeSheetID=" & lngEmployeeID, , , "NoTimeSheetID"
    Else
    MsgBox "No record"
    End If

    I am a little confused.
    Why is the variable lngEmployeeID set to TimeSheetID? Should the variable be lngTimeSheetID?
    If the form should open only if lngEmployeeID has value, why the "NoTimeSheetID" text for OpenArgs?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Hasher is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    May 2013
    Posts
    13
    Thank you June7 . That worked .Thank you for your help , code is very robust now . Thanks !!!!!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Conditional Formatting Problem
    By baileysw in forum Access
    Replies: 2
    Last Post: 11-21-2012, 06:23 PM
  2. DLookup Problem
    By alsoto in forum Queries
    Replies: 5
    Last Post: 02-23-2012, 12:22 PM
  3. Dlookup problem
    By metronometro in forum Queries
    Replies: 1
    Last Post: 01-07-2012, 12:17 PM
  4. Conditional Formatting Problem
    By DanKoz in forum Access
    Replies: 6
    Last Post: 10-23-2011, 12:57 AM
  5. conditional format problem in access
    By bold01 in forum Access
    Replies: 2
    Last Post: 02-10-2011, 11:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums