Results 1 to 12 of 12
  1. #1
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64

    DLookup Syntax

    Hello:



    I would like to populate the ControlSource for a form field using DLookup. I have this, but it is not working...

    =DLookUp(Sum([TimeCards]![laborhours]),[TimeCards],[TimeCards]![Date]=[Forms]![frmTimeCard]![txtDate] And [TimeCards]![empid]=[Forms]![frmTimeCard]![txtEmpID])

    Thanks in advance!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Using Sum() like that is nonsensical. And the DLookup must have literal argument parameters in quote marks and arguments must provide field and table names to search in. A WHERE CONDITION argument is optional.

    Maybe you really want DSum.

    =DSum("[laborhours]", "[TimeCards]", "[Date]=#" & [txtDate] & "# And [empid]=" & [txtEmpID])

    Research use of domain aggregate functions (DLookup, DSum, DAvg, etc). Try Access Help or web. Here is one http://rogersaccessblog.blogspot.com...mystified.html

    Date is a reserved word. Should avoid reserved words as names.
    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.

  3. #3
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    Yes, DSUM is good.

    This seems logical (shorter version produced the same). but still resulting in #Error
    =DSum("[laborhours]","[TimeCards]"," [TimeCards]![Date] =#" & [Forms]![frmTimeCard]![txtDate] & "# And [TimeCards]![empid] =" & [Forms]![frmTimeCard]![txtEmpID])

    I tried this as well...
    =DSum("[laborhours]","[TimeCards]"," [TimeCards]![Date] =#" & [Forms]![frmTimeCard]![txtDate] & "# And [TimeCards]![empid] ='" & [Forms]![frmTimeCard]![txtEmpID]) & "'"

    And this, which produced not the #Error in the field, but blank data:

    =DSum("[laborhours]","[TimeCards]"," [TimeCards]![Date] =" & [Forms]![frmTimeCard]![txtDate] & " And [TimeCards]![empid] =" & [Forms]![frmTimeCard]![txtEmpID])

    Thanks for the help. I will read your link now.

  4. #4
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    To me, this seems to be the logical choice. By the way, I swapped the # for % because it is a linked SQL table. But it now produces #NAME in the result. #NAME, #ERROR, blank - what's the difference?

    =DSum("[laborhours]","[TimeCards]","[Forms]![frmTimeCard].[Date]=%" & [txtDate] & "% And [Forms]![frmTimeCard].[empid]='" & [txtEmpID] & "'")

    Thanks again...

  5. #5
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    So I figured it out...

    =DSum("[laborhours]","[TimeCards]","[Date]=#" & [Forms]![frmTimeCard].[txtDate] & "# And [empid]='" & [Forms]![frmTimeCard].[txtEmpID] & "'")

    But I need to alter it to look at different users, as it's giving me the same data for each user.

    I will keep you (the forum) posted.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    That expression should return data specific to each empid.

    Who is the user? Is that the empid?

    Is empid a text type field?
    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.

  7. #7
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    empid is the user, and is a text field. I need to check something else.

  8. #8
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    I needed to make some changes. Can you tell me why this is not working?

    Code:
    =DLookUp("complete","tblocalApprovalLog","empid = '" & [Forms]![frmTimeCard].[txtEmpID] & "' AND dayDate = '" & [Forms]![frmTimeCard].[txtDate] & '" & AND weekDate = '" & [Forms]![frmTimeCard].[txtPeriodStart] & " - " & [Forms]![frmTimeCard].[txtPeriodEnd] & "' ")
    The date fields are strings, not dates in the table.

    I am getting an error that says I may have entered an operand without an operator.

    Thanks.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Misplaced quote mark and & before AND weekDate, should be: & "' AND weekDate =

    Don't put the minus sign (-) within quote marks and drop the & on each side - in other words, do the arithmetic so only the result is concatenated.
    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.

  10. #10
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    Great Catch.

    Can you tell me, is there a good way to have the control source be this, and also be able to change the value? Complete is a boolean value tied to a textbox.

    Thanks again!

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Not by user input to the same control. Change what value? Requires code to save calculated result.
    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.

  12. #12
    sanderson is offline Advanced Beginner
    Windows 8 Access 2013
    Join Date
    Jul 2015
    Posts
    64
    Yes, thanks. I was somehow thinking I could do two things with one control.

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

Similar Threads

  1. dlookup syntax
    By markjkubicki in forum Access
    Replies: 3
    Last Post: 06-25-2014, 06:13 PM
  2. Dlookup syntax
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 03-05-2014, 06:00 PM
  3. IIf and dlookup syntax
    By AndycompanyZ in forum Forms
    Replies: 3
    Last Post: 06-28-2011, 02:47 PM
  4. DLookup() syntax????
    By jgelpi16 in forum Programming
    Replies: 3
    Last Post: 03-30-2011, 09:14 AM
  5. dlookup() syntax
    By markjkubicki in forum Programming
    Replies: 4
    Last Post: 08-24-2010, 12:31 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