Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    tfxobrien is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2016
    Posts
    7

    fill field with data from another table by lookup


    I have a parameter query using date between to select records within that date from Table1. That works fine.
    But there are 3 fields that need to lookup into another table and display the second field from that table rather than the original query field
    EXample;
    Table1 Name DOB, date, C1(number),C2(number),C3(number)
    Table2 Price, Service
    use the value in table1.C1 to match the value in table2 but then display Table2.Service rather than the number.
    C1,C2,C3 could be 0 and then it should not dispaly any service.
    I've played with it for a few days without any luck.
    BTW: This is not my db, I'm trying to help a friend.
    Thanks

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    rather vague description, but sounds like a) your data in table1 is not normalised and b) you would need to link using a left join between C1 and price in table2, c2 to price in table2 and c3 to price in table2 (3 joins, 2 aliases of table2, just drag them onto the query grid)

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    use the value in table1.C1 to match the value in table2 ....
    which field

    Can you post a copy of the db
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    which field
    according to the OP, there are only 2 - price and service. And since he wants to bring back service, it has to be price

  5. #5
    tfxobrien is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2016
    Posts
    7
    Sorry for being vague.Yes, the data is not normalized. It is a medical db written in 2005 with Access 2003.
    Table1 is the Patient record and Table2 is the services with the price
    I understand your idea.
    For some reason the creator used the price of the service to be the link between table1 and table2.
    The c1,c2,c3 values are the price of the service and are stored in table1.
    When c1,c2,c3 has a value other than 0 I need to lookup for that price in table2 and display the table2.service as a field in the table1 query.
    How would I create the 3 left joins? Would I do that on table1 previous to the query or on the query?

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    on the query - to create a left join, make a 'normal' join then right click and select the option something like 'select all records from table1 and those records from table2 where they exist'

  7. #7
    tfxobrien is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2016
    Posts
    7

    table1 and table2 posted as excel files

    Table1.ziptable2.zip
    In table1 the field Colonoscopy is C1, Colonoscopy2 is C2, etc.
    As you will see the price is 805 in table1.
    I would like the query to show the Service from table2 which is "G0121 Colon SCR".
    Thanks

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    what you have provided is the equivalent of saying - my car is not working, here is a picture of my car.

    there is no table 1, and please provide your db if you want us to make suggestions - in a copy of your db, remove all unrequired tables, queries, forms reports, macros and modules. Then in the remaining tables remove or modify any confidential data.

    Finally compact and repair the copy db, zip it and upload it

  9. #9
    tfxobrien is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2016
    Posts
    7

    Patientdb.zip

    Here is the mdb.
    I followed your directions to create it.Patientdb.zip

  10. #10
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    copy and paste this code into a new query

    Code:
    SELECT table1.[Procedure ID], table1.[Patient ID], table1.Hospital, table1.[In/Out Patient], table1.[Procedure 1], table1.[Procedure 2], table1.SubProc, table1.SubPro2, table1.SubPro3, table1.SubPro4, table1.SubPro5, table1.SubPro6, table1.SubPro7, table1.SubPro8, table1.Comment, table1.Diagnosis, table1.[Diagnosis 2], table1.[Diagnosis 3], table1.[Diagnosis 4], table1.Date, table1.Colonoscopy, table1.EGD, table1.ERCP, table1.[Small Bowel], table1.MISC, table1.Colonoscopy2, table1.Colonoscopy3, table1.EGD2, table1.EGD3, table1.ERCP2, table1.ERCP3, table1.[Small Bowel2], table1.[Small Bowel3], table1.MISC2, table1.MISC3, Colonoscopy.Service, Colonoscopy_1.Service, Colonoscopy_2.Service
    FROM ((table1 LEFT JOIN Colonoscopy ON table1.Colonoscopy = Colonoscopy.Price) LEFT JOIN Colonoscopy AS Colonoscopy_1 ON table1.Colonoscopy2 = Colonoscopy_1.Price) LEFT JOIN Colonoscopy AS Colonoscopy_2 ON table1.Colonoscopy3 = Colonoscopy_2.Price
    WHERE (((table1.Date) Between [start] And [end]));
    You also need to correct the datatype for the Colonoscopy field - it is type text and should be type number (long integer).

    Other things:

    1. using non alpha numeric characters in field names is a bad idea (In/Out Patient). , it is prone to creating misleading errors
    2. using reserved words such as Date is also a bad idea - Date is a function that returns today as a date , it is prone to creating misleading errors
    3. although you can have spaces in field names, it is prone to creating misleading errors
    4. You really should look at normalising your data - Procedures, SubProc, Diagnosis, Colonoscopy, Small Bowel, Misc are all indicative of a poorly designed database which requires much more complex queries, form and report design etc, perhaps so complex they cannot even be created.

  11. #11
    tfxobrien is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2016
    Posts
    7
    I want to thank you for your help. Unfortunately, it is not my db. I was asked to see if I could generate some specific reports for a doctor friend. His son wrote the system in 2004 with Access 2003 and is long gone. What was most helpful were your 4 comments. I live in a linux (Centos) world and now will probably make csvs of all the tables and bring them over to my system.
    Also, unfortunately, the code above gave type mismatch errors. I tried to make repairs but no luck. Fields such as Colonoscopy_1.Price, I think, should be Colonoscopy1.Price.
    At any rate, I'm still interested in knowing how to present the Service field on the query for at least one simple instance to understand how to present a field from another table in that manner, if you are up for that?
    I did a lot of seaching on the web prior to putting the request on the forum. I found many discussions of creating a calculated field but none that match my needs which were creating a field and getting the data form another table based on a lookup. I'm sure I'm not using the proper Access lingo here. Even if you could point me to a link that shows this idea. It certainly does not have to be the tables I sent. I realize that it would be automatic if the tables were linked.
    Thanks again for your time. I will pay it forward in my world.

  12. #12
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    the code above gave type mismatch errors
    because you haven't fixed this
    You also need to correct the datatype for the Colonoscopy field - it is type text and should be type number (long integer).
    Fields such as Colonoscopy_1.Price, I think, should be Colonoscopy1.Price.
    No, Colonoscopy_1 is an alias for the Colonoscopy table - because you have to refer to it three times for your three fields. Having copy and pasted the code I provided into the query sql window you can go back to the query view to see 'what it looks like'

  13. #13
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    if you can't change the field type for the Colonoscopy field type then this will work - just copy and paste it since it is based on your original query

    Code:
    SELECT table1.[Procedure ID], table1.[Patient ID], table1.Hospital, table1.[In/Out Patient], table1.[Procedure 1], table1.[Procedure 2], table1.SubProc, table1.SubPro2, table1.SubPro3, table1.SubPro4, table1.SubPro5, table1.SubPro6, table1.SubPro7, table1.SubPro8, table1.Comment, table1.Diagnosis, table1.[Diagnosis 2], table1.[Diagnosis 3], table1.[Diagnosis 4], table1.Date, table1.Colonoscopy, table1.EGD, table1.ERCP, table1.[Small Bowel], table1.MISC, table1.Colonoscopy2, table1.Colonoscopy3, table1.EGD2, table1.EGD3, table1.ERCP2, table1.ERCP3, table1.[Small Bowel2], table1.[Small Bowel3], table1.MISC2, table1.MISC3
    FROM ((table1 LEFT JOIN Colonoscopy ON val(table1.Colonoscopy) = Colonoscopy.Price) LEFT JOIN Colonoscopy AS Colonoscopy_1 ON table1.Colonoscopy2 = Colonoscopy_1.Price) LEFT JOIN Colonoscopy AS Colonoscopy_2 ON table1.Colonoscopy3 = Colonoscopy_2.Price
    WHERE (((table1.Date) Between [start] And [end]));
    The change is in red. Note that as a result of this change, the query cannot be viewed in the query builder

  14. #14
    tfxobrien is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2016
    Posts
    7
    Changed the Colonoscopy field type from text to Number and it worked.
    Thanks for being so patient.

  15. #15
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    no problem - you can see the query view to see the visualisation?

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

Similar Threads

  1. Auto fill a field from multiple lookup
    By shahemaan in forum Forms
    Replies: 4
    Last Post: 06-13-2014, 04:00 PM
  2. Replies: 3
    Last Post: 03-07-2014, 10:39 AM
  3. Replies: 6
    Last Post: 09-10-2012, 07:19 AM
  4. Replies: 5
    Last Post: 06-14-2012, 08:30 AM
  5. Replies: 5
    Last Post: 01-20-2011, 11:36 PM

Tags for this Thread

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