Results 1 to 10 of 10
  1. #1
    frostnuts is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5

    Dynamic button lables using Dlookup

    Hey guys. I've been having problems getting Dlookup to work. I'm trying to create dynamic lables for buttons on a form. The source for the lables would be a table named MainMenu. After racking my brain for hours, I came up with this bit of code:



    =[Locations].[Caption]=DLookUp("[Caption]","[MainMenu]","[ID]=1")

    Locations is the name of the button object. Caption is the name of the field in the table i want to copy from and the table's name is, again, MainMenu. I want to copy from the first record for that button so I put Id=1 as the criteria. I entered this code under the form's OnLoad event handler. I selected expression builder and typed it in there. I checked everything ten times already. I'm convinced my syntax is correct as per the microsoft access help website. I've with and without the brackets so I dont think thats the problem. For the life of me, I cannot figure out why this is not working! Please help. Thanks in advance.
    Last edited by frostnuts; 03-17-2013 at 08:09 AM.

  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,518
    If you want to use code it would go here:


    http://www.baldyweb.com/FirstVBA.htm


    and look like

    Me.[Locations].[Caption]=DLookUp("[Caption]","[MainMenu]","[ID]=1")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    frostnuts is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    Unfortunately it didn't work. It gave an error saying it could not find the object {Me}. Also I want the code to execute when the form opens not when the buttons are pressed. Thats why I placed the code under the form's OnLoad event handler. Any further help would be appreciated.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    a table named MainForm
    the table's name is, again, MainMenu.
    Is this an issue????

  5. #5
    frostnuts is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    Ok so I tried inputing this: =[Me].[Locations].[Caption]=DLookUp("[Caption]","[MainMenu]","[ID]=1")

    And it says the object does not contain the automation object 'Me'. It also goes on to say:

    This error occurs when an event has failed to run because the location of the logic for the event cannot be evaluated. For example, if the OnOpen property of a form is set to =[Field], this error occurs because a macro or event name is expected to run when the event occurs.

    But about the code I typed in? It sounds like its in the wrong place. I tried it with the OnLoad and the OnClick event handlers.

  6. #6
    frostnuts is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    Quote Originally Posted by orange View Post
    Is this an issue????
    Sorry about that! The tables name is MainMenu.

  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,518
    I gave you that link not to specify an certain event, but to show you how to get into VBA. You can use the load event, but the code needs to be in VBA. Me is only valid in VBA, which is why you get the error.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    if the code is behind the form,
    =[Me].[Locations].[Caption]=DLookUp("[Caption]","[MainMenu]","[ID]=1")
    should work

    However, if its in a module...
    = forms!Location.Caption=DLookUp("[Caption]","[MainMenu]","[ID]=1")


    me is always bothering me. I try to stick to forms! if at all possible.



  9. #9
    frostnuts is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Mar 2013
    Posts
    5
    Quote Originally Posted by pbaldy View Post
    I gave you that link not to specify an certain event, but to show you how to get into VBA. You can use the load event, but the code needs to be in VBA. Me is only valid in VBA, which is why you get the error.
    Thanks Pbaldy!! Your a life saver. I never would have thought to put the code in VBA. Thanks again! This thread is officially SOLVED!

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Happy to help! Welcome to the site by the way!
    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. Dynamic crosstab report sorting headings & dlookup
    By chrisangk in forum Programming
    Replies: 9
    Last Post: 01-14-2013, 02:37 PM
  2. Replies: 5
    Last Post: 12-19-2012, 09:51 AM
  3. Replies: 1
    Last Post: 10-18-2011, 07:10 PM
  4. Dynamic Form, Dynamic labels/ captions?
    By JFo in forum Programming
    Replies: 15
    Last Post: 10-12-2011, 08:33 PM
  5. Programming lables to hide again.
    By 95DSM in forum Programming
    Replies: 3
    Last Post: 12-30-2010, 01:43 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