Results 1 to 14 of 14
  1. #1
    crimedog is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    141

    Dlookup Works in Controlsource - but not in VBA

    in the control Source I have:
    Code:
    =DLookUp("[RevBonus]","[Tbl2_MgrBonus]","[BonusMo]=" & [TxtMo1] & "And [BonusYr]=" & [TxtYear] & "And [MgrID]=" & [TxtMgrID])
    And this works.

    When I have tried to translate it to VBA in On Load
    Me.RevAttain1.ControlSource=



    I get errors - I have tried what I know - closest I can come gives me a Name? error - or a VBA error

    Can someone translate it for me?

  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
    If you're trying to put the result of the DLookup() there, use the Value property, not ControlSource.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    crimedog is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    141
    When I do that I get You entered an expression that has no value

  4. #4
    crimedog is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    141
    Code:
    Me.RevAttain1.Value = DLookup("RevBonus", "Tbl2_MgrBonus", "BonusMo" = [TxtMo1] And "BonusYr" = [TxtYear] And "MgrID" = [TxtMgrID])
    one verson - getting the no value error

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Note the differences in how you did it:

    DLookUp("[RevBonus]","[Tbl2_MgrBonus]","[BonusMo]=" & [TxtMo1] & "And [BonusYr]=" & [TxtYear] & "And [MgrID]=" & [TxtMgrID])
    DLookup("RevBonus", "Tbl2_MgrBonus", "BonusMo" = [TxtMo1] And "BonusYr" = [TxtYear] And "MgrID" = [TxtMgrID])
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    crimedog is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    141
    yeah, I tried it both ways - same error

  7. #7
    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 first one is your working one from the control source. The second is completely messed up. Try the syntax of the first. You may need to use the names of textboxes if different.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Also, I would try adding a space in front of each of the and's"

    DLookUp("[RevBonus]","[Tbl2_MgrBonus]","[BonusMo]=" & [TxtMo1] & " And [BonusYr]=" & [TxtYear] & " And [MgrID]=" & [TxtMgrID])

    And if [TxtMo1] is a control on a form, I would use the Me keyword:
    DLookUp("[RevBonus]","[Tbl2_MgrBonus]","[BonusMo]=" & Me.[TxtMo1] & " And [BonusYr]=" & Me.[TxtYear] & " And [MgrID]=" & Me.[TxtMgrID])

  9. #9
    crimedog is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    141
    it is not giving a syntax error - it is saying "you entered an expression that has no value"
    (I reverted to the original syntax)

  10. #10
    crimedog is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    141
    Me.RevAttain1.Value = DLookup("[RevBonus]", "Tbl2_MgrBonus", "[BonusMo] =" & Me.[TxtMo1] & " And [BonusYr] =" & Me.[TxtYear] & " And [MgrID] =" & Me.[TxtMgrID])

    Returning the No value error (It does return a value when it is in the control source)

  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
    Can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try with one criteria.

    Code:
    Me.RevAttain1.Value = DLookup("[RevBonus]", "Tbl2_MgrBonus", "[BonusMo] =" & Me.[TxtMo1])
    If a value is returned, add another criteria:

    Code:
    Me.RevAttain1.Value = DLookup("[RevBonus]", "Tbl2_MgrBonus", "[BonusMo]  =" & Me.[TxtMo1] & " And [BonusYr] =" & Me.[TxtYear])
    Then add the third criteria. Trying to see if one of the criteria is causing the problem........

  13. #13
    crimedog is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Dec 2013
    Posts
    141
    Got it - I was getting the NO Value Error because I was pointing to Text Boxes in the report that was loading - so in effect My On Load Action is happening before the Text Boxes are filled.
    I pointed it instead to Text boxes on a form that is open in the background - works like a charm


    Thanks Guy!!

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 1
    Last Post: 06-21-2015, 07:58 AM
  2. Replies: 3
    Last Post: 03-26-2015, 06:50 PM
  3. Replies: 1
    Last Post: 11-14-2014, 05:12 PM
  4. ControlSource problem
    By akhmadahdiyat in forum Programming
    Replies: 3
    Last Post: 11-29-2013, 12:06 PM
  5. DLookup as ControlSource for a text box?
    By GraeagleBill in forum Forms
    Replies: 4
    Last Post: 03-29-2013, 05:09 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