Results 1 to 5 of 5
  1. #1
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218

    Text box to display value based on a query

    I've tried setting the control source of a text box to get its values from a query by doing txtBuildingName.controlsource = "select tblbuildings.buildingfk, tblbuuldings.buildingname from tblbuildings where buildingfk = [txtBuildingID].



    It's not working.

    Basically, I need txtBuildingName to display the name of the building id which is stored in txtBuildingID. So if txtBuildingID is "1" and that is listed in tblbuildings as building "A" then "A" should show up in txtBuildingName.
    Last edited by ittechguy; 10-06-2015 at 05:33 PM.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    What is the Control Source of the Form/Report?
    If it is the query that the field resides in, you can just click and drag that field from the Field Box to your Form/Report.

    If it is not in the Control Source that the Form/Report is based on, you can either:
    - add it to the Query that the Form/Report is based on
    or
    - use the DLOOKUP function to look it up from another Table/Query
    (see http://www.techonthenet.com/access/f...in/dlookup.php)

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,643
    Or if the ID is (or can be) a combo box this is another option:

    BaldyWeb - Autofill
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    ittechguy is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Aug 2015
    Posts
    218
    Idk why I didn't even think about adding building name and room name to the query.

    What is the preferred method? The query is already getting long and it's the recordsource for the entire form. I have to add a couple more tables to it so it's going to get even larger. But I guess adding names to the query is likely better than dlookup.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Yes, DLOOKUPs can be tricky/fluky (and subject to errors), so I would say adding them to the query is preferential.
    I wouldn't be too concerned about the size of your query, other than that if it gets too long, it may be indicative that maybe you should be using some Form/Subform or Report/Subreport relationships.

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

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2015, 07:06 PM
  2. Replies: 3
    Last Post: 06-16-2015, 10:32 AM
  3. Replies: 8
    Last Post: 06-23-2014, 12:13 PM
  4. Replies: 1
    Last Post: 06-09-2014, 09:30 AM
  5. Replies: 3
    Last Post: 10-23-2009, 05:03 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