Results 1 to 9 of 9
  1. #1
    naylorg is offline Novice
    Windows 11 Access 2016
    Join Date
    May 2025
    Posts
    6

    Accessing indivual recordes on a subform

    Hi,



    I have a simple setup where there are two tables, a main table and a secondary table linked to the main table, where for each record in the main table there can be one or more records in the secondary table.

    I have built a simple form with a subform setup where the records on the subform are in the usual grid format.

    I want to let the user select a row on the grid by clicking on the row number, hence highlighting the whole row, and then clicking on a button on the main form, the code attached will read the id (displayed in the first column of the grid), what code do I need and in what format to access this id field? (The main form is called "MainForm" and the sub form is called "SubForm")

    Thanks

    Graham Naylor

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What do you want to do with selected ID?

    Need to reference subform container control name. I always name container different from object it holds, like ctrDetails.

    Code behind button:

    Me.subformcontainername!fieldname

    or

    Me.subformcontainername.Form.controlname
    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.

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Graham,

    Can you tell us a little more about your set up? You have a form and a subform-- what is the subject matter involved? How are your tables related in "business terms"--- owners and cars; warehouses and products; cities and warehouses,, sort of idea. I'm trying to determine what is in the subform that, once selected, requires a button click on the parent form to display some value from the subform.
    I'm not challenging your requirement, just curious and would like more context.

  4. #4
    naylorg is offline Novice
    Windows 11 Access 2016
    Join Date
    May 2025
    Posts
    6
    Hi,

    The database i for a quick example for a local classic car club, the main form will show the member's details (name, address, membership no. etc) and the subform will show the car(s) owned by the member shown on the main form. Unfortunately the details about each car will need a form of their own due to number of fields, picture etc. So what I want to do is display the basic information about each car (Registration, make, model etc) on the subform, then let the user select the car they are interested i by selecting the relevant row on the subform grid, then click on a button that will display the full car details on a pop-up form.

    In the code the one thing that is confusing me is referencing the data on the selected row, all I need is the format of the code that i need to pass the registration number into a variable.

    for the example just use the following:

    Main Form = fMainForm
    Sub Form = sSubForm
    Registration Number (Variable) = sRegNo

    Thanks

    Graham Naylor

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    This syntax should work from anywhere:
    myVariable = forms![form name here].[subform control name here].Form.[control name here]

    The control name would be the one that contains your record id (which normally you don't show to users)
    In some cases you can simplify that code, but it depends on where the action is being run from.

    EDIT - I removed a statement that might have referred to a method that doesn't exist. Will have to check it out.
    EDIT2 - OK, there is a double click event for a form that works on a subform record. For a moment I was thinking that a form with controls can't take the focus and that might matter. I'm not using Access enough to stay on top of things.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by naylorg View Post
    Hi,

    The database i for a quick example for a local classic car club, the main form will show the member's details (name, address, membership no. etc) and the subform will show the car(s) owned by the member shown on the main form. Unfortunately the details about each car will need a form of their own due to number of fields, picture etc. So what I want to do is display the basic information about each car (Registration, make, model etc) on the subform, then let the user select the car they are interested i by selecting the relevant row on the subform grid, then click on a button that will display the full car details on a pop-up form.

    In the code the one thing that is confusing me is referencing the data on the selected row, all I need is the format of the code that i need to pass the registration number into a variable.

    for the example just use the following:

    Main Form = fMainForm
    Sub Form = sSubForm
    Registration Number (Variable) = sRegNo

    Thanks

    Graham Naylor
    For that, just pass in the subform ID as the WHERE parameter for the popup form?
    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

  7. #7
    naylorg is offline Novice
    Windows 11 Access 2016
    Join Date
    May 2025
    Posts
    6
    Works great, thanks

    Graham

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by naylorg View Post
    Works great, thanks

    Graham
    What works great?
    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

  9. #9
    naylorg is offline Novice
    Windows 11 Access 2016
    Join Date
    May 2025
    Posts
    6
    The solution posted by Micron which I received and tried first.

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

Similar Threads

  1. VBA Syntax for Accessing Record from Subform
    By Ramtrap in forum Modules
    Replies: 11
    Last Post: 11-14-2017, 01:36 PM
  2. Replies: 2
    Last Post: 06-30-2016, 03:13 AM
  3. Replies: 0
    Last Post: 03-28-2016, 11:51 AM
  4. Replies: 2
    Last Post: 07-25-2012, 05:03 PM
  5. Accessing subform fields
    By nkenney in forum Forms
    Replies: 1
    Last Post: 04-21-2009, 10:10 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