Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58

    Dlookup to match two criteria and return value

    I have created some code and can not seem to get it to work correctly.



    If the Ref Workload field is <> 0 the code is suppose to return the Ref Workload value from the Employee Table where the Employee ID and Date of Work field matches the same as the currrent record.

    Employee ID: Number field
    Date of Work: Date/Time field
    Ref Workload: Number field

    Code:
     
    Private Sub Employee_ID Change()
    Dim RefWorkload As Integer
    RefWorkload = Nz(DLookup("Ref_Workload", "Employee Table", "[Employee ID] = " & Forms![Employee Table]![Employee ID] & " AND [Date of Work] =#" & Forms![Employee Table]![Date of Work] & "#"), 0)
    If Len(Me.Ref_Workload & "") <> 0 Then
    Me.Ref_Workload = RefWorkload
    End If
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The table name may need to be bracketed due to the inadvisable space.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    I have put the table name in brackets....I am getting a Run Time Error 13 (Type Mismatch) on the line I bolded below.


    Code:
     
    Private Sub Employee_ID Change()
    Dim RefWorkload As Integer
    RefWorkload = Nz(DLookup("Ref_Workload", "[Employee Table]", "[Employee ID] = " & Forms![Employee Table]![Employee ID] & " AND [Date of Work] =#" & Forms![Employee Table]![Date of Work] & "#"), 0)
    If Len(Me.Ref_Workload & "") <> 0 Then
    Me.Ref_Workload = RefWorkload
    End If
    End Sub

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You're sure of the data types in the table? Can you post the db?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    pbaldy,

    I have double checked the data types again to be sure that I had posted them correct. I would upload the DB for you but I am currently at work and can not upload due to admin security preventing uploads. If you have any other thoughts I can try if not then I can upload when I get home.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, the syntax looks correct for those data types, and that error usually indicates a data type mismatch. What kind of value does Ref_Workload contain? Is it possible it's too large for Integer (roughly 32k)?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    Ref_Workload contains numbers ranging between 1 and 500.

  8. #8
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    I looked at Ref_Workload in the table and it is set to Number (Long Integer) not sure if that makes a difference.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I doubt it matters but you can try changing this to match:

    Dim RefWorkload As Long

    Other than that I'm stumped, so if you can post the db later it usually helps to be able to "put your hands on it".
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    Ok after I added the brackets and did a compact and repair I am no longer getting the error message....but it appears that RefWorkload is being returned as 0 results matching....even when I have records that have the same Employee ID and Date of Work. I am so lost.

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would set a breakpoint and make sure the values coming from the form are what you expect:

    http://www.baldyweb.com/Debugging.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    I have not used the Locals Window before and the only thing I saw was the value for RefWorkload = 0.

    I could not find the Employee ID and Date of Work.

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If you set a breakpoint so that the code stops during execution, you should be able to hover over those with the mouse and see the value, or type things like this in the Immediate window:

    ?Forms![Employee Table]![Employee ID]

    which should return the value from the form.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    randolphoralph is offline Advanced Beginner
    Windows XP Access 2002
    Join Date
    Dec 2008
    Posts
    58
    Ok I have found a computer I can upload the DB from.....

    I also, noticed that if I enter the Employee ID and then go back and change it I still get the Data Type Mismatch.

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It seems to be returning correct results for me, but I did change one thing. You have it in the change event, which fires with each keystroke and unless you specify the .Text property, uses the previous value in the control. You want the after update event. Try that and see where we are.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Unstack Data and Match with Unique ID
    By lukejones in forum Queries
    Replies: 7
    Last Post: 02-24-2014, 08:33 AM
  2. data type mis match
    By cowboy in forum Programming
    Replies: 3
    Last Post: 03-12-2010, 11:54 AM
  3. Query to match ID's
    By Shag84 in forum Access
    Replies: 2
    Last Post: 09-06-2009, 08:13 PM
  4. Match up table using only a few charecter?
    By bangemd in forum Queries
    Replies: 5
    Last Post: 06-05-2009, 04:15 AM
  5. Finding data that doesn't match
    By dlhayes in forum Queries
    Replies: 1
    Last Post: 11-11-2006, 08:14 PM

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