Results 1 to 4 of 4
  1. #1
    bole is offline Novice
    Windows 10 Access 2019
    Join Date
    Dec 2023
    Posts
    1

    Question Autofill a foreign key field


    It's a simple database with two tables. One patients table and one health reports table. It's a one to many relationship (patients - health reports).


    I have a split form that's based on the patients table and displays data about the patient and there is button that opens health reports only for that patient (it filters the form). In the case that the patient doesn't have any health reports assigned, I want that button to open a new record of the health reports form with the patient ID field already filled so that it automatically assigns it to the patient. Also when I have the health reports form open, I want a button that opens a new record of a health report with the patient ID of the health report that was initially opened.


    If you have a solution to the problem, didn't understand the problem or have a better way of designing this database please inform me.





    I'm tight on time so I did post the same question on reddit: https://www.reddit.com/r/MSAccess/co..._fill_a_field/

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940

    Thumbs up

    Use the PK as the Where criteria on the open form.
    Also pass it in as OpenArgs to populate the FK field on BeforeInsert event.

    Thank you for the notice on the cross-post.:
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Could place Reports form as a subform on Patients form and no VBA needed.

    If you still want button to put user into Reports subform on new record row:

    Me.Reports.SetFocus
    DoCmd.GoToRecord , , acNewRec
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I think the word "report" here does not refer to an Access report:
    there is button that opens health reports only for that patient (it filters the form)
    I want that button to open a new record of the health reports form
    Anyway, there are decent answers at the other thread - I guess as long as it's understood that the 2nd form is based on a query or table that uses the id from the current record of the first form. A problem I think I see with having a default value setting on form2 is that errors will probably be raised whenever that form is opened and form1 is not open. Also, form2 would likely have to be modal, otherwise the current record on form1 could be changed by the user while form2 is still open.
    Last edited by Micron; 12-26-2023 at 09:36 AM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 1
    Last Post: 12-28-2016, 11:39 AM
  2. Replies: 1
    Last Post: 02-14-2015, 02:29 AM
  3. Replies: 2
    Last Post: 04-19-2012, 11:29 AM
  4. Replies: 1
    Last Post: 02-20-2012, 01:02 PM
  5. Replies: 11
    Last Post: 11-09-2011, 11:25 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