Results 1 to 8 of 8
  1. #1
    staton101 is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    4

    Pre-populate fields on a Form from last record based on entry of field

    Hi, I'm fairly new to Access 2016 and VBA and hope that I can get some help please.


    I have a financial transaction Form and when entering a Vendor/Payee from a Combobox I want to fetch the latest Financial transaction entered for that Vendor/Payee and return 2 fields to populate onto the displayed Form. I think using 'On change' event on the Vendor/Payee field would be the way to go but I'm not sure about the coding required.
    Can you help?

  2. #2
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    staton101,

    Assuming that the fields you want are in the same table as the combo box is referencing, ensure that you include those fields in the combo box. I would use the AfterUpdate event. Then in the fields you want populated on the form you want populated use VBA in the AterUpdate event to populate those referring to the column of the combo box.

    If you could attache a copy of the database it would be much easier to assist.

    Dave

  3. #3
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    I would use the after update event for a combo, and ensure that you bring in the 2 fields required as hidden columns in the combo row source query.
    You can the set the values on the form using (assume the Vendor is the first bound column)

    Code:
    Me.YourControl1 = Me.comboVendor.Column(1)
    Me.YourControl2 = Me.comboVendor.Column(2)
    Where comboVendor is your combo control name. Column numbering starts at (0)
    YourControl1 and 2 are the controls you want to set the value in.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    staton101 is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    4

    The fields come from the Financial Transaction (TransEntries) table

    Quote Originally Posted by Dave14867 View Post
    staton101,

    Assuming that the fields you want are in the same table as the combo box is referencing, ensure that you include those fields in the combo box. I would use the AfterUpdate event. Then in the fields you want populated on the form you want populated use VBA in the AterUpdate event to populate those referring to the column of the combo box.

    If you could attache a copy of the database it would be much easier to assist.

    Dave
    I want to retrieve 2 fields from the last transaction created for that Vendor/Payee (as they would probably be the same info) to save me from typing it in.
    So I need to trigger a read of TransEntries table for the last entry for that Vendor and bring back Category and TransNote into the Form displayed.
    Any assistance would be appreciated.
    Click image for larger version. 

Name:	TransEntries.PNG 
Views:	8 
Size:	26.7 KB 
ID:	38413
    Last edited by staton101; 05-15-2019 at 01:37 AM. Reason: Adding table details

  5. #5
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You don't need a trigger, you need a query or possibly two, joined to your vendor table.

    This is the simple route;
    Write a query that gets you the last transaction for all vendors.
    Save that as qryLastTrans

    In your combobox row source, add that query to your vendors table, and use an left join to link the vendor id's from the query and the table.
    Add the fields you need , e.g. the vendor ID , Vendor Name , LastCategory, transnote.

    Now your combo has the data required and can be populated on your form using the code above.
    Try it, if you get stuck then post back where you got stuck and what you have tried.

    Side note: I notice you are storing the vendor as a text field, you may be better storing an ID number if that's what is in your vendor table.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    staton101 is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    4

    Help with query..

    Quote Originally Posted by Minty View Post
    You don't need a trigger, you need a query or possibly two, joined to your vendor table.

    This is the simple route;
    Write a query that gets you the last transaction for all vendors.
    Save that as qryLastTrans

    In your combobox row source, add that query to your vendors table, and use an left join to link the vendor id's from the query and the table.
    Add the fields you need , e.g. the vendor ID , Vendor Name , LastCategory, transnote.

    Now your combo has the data required and can be populated on your form using the code above.
    Try it, if you get stuck then post back where you got stuck and what you have tried.

    Side note: I notice you are storing the vendor as a text field, you may be better storing an ID number if that's what is in your vendor table.
    I have created a simple query that retrieves TransEntries SQL
    SELECT DISTINCT TransEntries.[ID], TransEntries.[TransDate], TransEntries.[Vendor], TransEntries.[Category], TransEntries.[TransNote]FROM TransEntries
    ORDER BY TransEntries.[TransDate] DESC;
    But on running still returns all the records in TransEntries and not unique Vendor entries.
    Also, the Vendor table doesn't have ID in it, the key is Vendor which will be unique.
    thanks..

  7. #7
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Okay - you are getting closer.
    Code:
    SELECT Max(TransEntries.[TransDate]), TransEntries.[Vendor], TransEntries.[Category], TransEntries.[TransNote]FROM TransEntries
    GROUP BY TransEntries.[Vendor], TransEntries.[Category], TransEntries.[TransNote]
    
    Try this as a query and you will see that it nearly gets you what you want.
    Hint: As you have multiple categories you will need to actually need another sub query to just return the last TransactionID grouped by vendor and link that back to get the categories and notes.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    staton101 is offline Novice
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    4
    OK, I can see how the query retrieves the entries grouped by Vendor, will the left join be part of the query?
    Also, if I add this query to the combobox row source in my Form won't this cause an issue with 'On not in list' event (which I forgot to mention before), which then inserts a Vendor in the Vendors table if it doesn't exist.
    Not sure I need to another sub query as I can use the Category from TransEntries.

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

Similar Threads

  1. Replies: 7
    Last Post: 10-13-2016, 12:32 PM
  2. Replies: 2
    Last Post: 12-04-2013, 05:38 PM
  3. Fields populate based on another field
    By jlclark4 in forum Forms
    Replies: 3
    Last Post: 12-27-2011, 05:21 PM
  4. Populate one field based upon another fields data
    By BigBrownBear in forum Queries
    Replies: 1
    Last Post: 03-23-2010, 04:27 PM
  5. Replies: 3
    Last Post: 10-05-2009, 07:22 AM

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