Results 1 to 9 of 9
  1. #1
    hfreedman1957 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2017
    Posts
    35

    Which fields to include in a Query

    Hi,

    Simple example here to apply proper structure to my much more complex database.
    Two tables, Students and Classes.
    Classes table has two fields - Class_ID (primary key), Class_Name
    Students Table has three fields - Student_ID, Student_Name, Class_ID

    I'm setting up a query based on the Students table and add both tables and establish he relationship from Class_ID in the Students table to Class_ID in the Classes table (one-to-many).

    My question is which fields should I include in the query - Students.Class_ID and/or Classes.Class_ID and/or Classes.Name in order to do the following for forms and reports with the query as the record source.

    1. Display Class_Name from Classes table (easy)
    2. Sort records in form based on Class_Name
    3. Add new records to the Students table from a form
    4. In VBA code from a form button event, add multiple Student records using the Class_ID from the current record in focus




    OR should I just be storing the class name itself in the Students record once I pick the class from the Classes table on the form using a combo box?
    Again, this is a very simple example. My db has a table that needs to select and store data from from many other tables. I'm wondering if I should be keeping the reference ID from those tables or store the actual data. Storing the actual data causes duplication of data (a waste) and causes grief if field values need to change, but how much grief am I causing myself by referencing ID's to display field names and creating new records? Any advice would be appreciated. Thanks so much!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Can each student have multiple classes? Can each class have multiple students? If yes to both then this is a many-to-many relationship and require a third 'junction' table to associate students with classes.

    If you want to allow each student to be in only one class (don't care about prior years?) then only save classID into Students.

    A form can enter new record for only 1 table. Have a form bound to Students and select class from combobox.

    For a report, include all fields in query, apply filter/sort criteria to whichever you want.

    Item 4 is not really practical and doesn't really make sense. If you want to change the ClassID for existing students, still have to select each student that needs to be changed. This is essentially making selection in classes combobox. If you want to add new students, still have to enter all student info, selecting class is just part of new record entry.
    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
    hfreedman1957 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2017
    Posts
    35
    My real DB has nothing to do with classes and students, that was just an example, and I guess not a good one. Mine is a one-to-many relationship. Your last statement answered my question though. I will just store the ID's. Thanks!

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Change table and field names, principles still apply. Edited my reply while you were reviewing.
    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.

  5. #5
    hfreedman1957 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Feb 2017
    Posts
    35
    Thanks! This is very helpful. Yep, I'll only be updating one table in a form. Much appreciated!

  6. #6
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    A form can enter new record for only 1 table.
    I'm confused by that statement. You're saying if a query is updatable and is based on 2 or more tables, only one of those tables can be updated, appended to or deleted from? I realize that it's not uncommon to refer to a query as a table (of data, at least) but I don't get the impression that was the context of the word "table" in the answer.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    I should qualify that statement. If tables have parent/child relationship, can't really use one query/form to add new records to both tables. Certainly not if referential integrity is enforced.

    It's my experience that parent record must be committed to table before child records can be created, and doing that in one query/form isn't practical. If it were, we wouldn't need subforms or subdatasheets. Picture a 1-to-many or many-to-many relationship and trying to do data entry in a single query/form.

    A form RecordSource can include other tables (lookups) but would not want to allow add/edit/delete of those tables via controls on that form (exception of combobox NotInList event). Including them would only be for purpose of displaying associated info.

    And I didn't mention editing or deleting. Those actions have their own pitfalls.
    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.

  8. #8
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Picture a 1-to-many or many-to-many relationship and trying to do data entry in a single query/form.
    Agreed. Not even sure you could, for how would you represent the many side with one form (assuming we take the position that a subform on a form is not really one form)?
    One to one - not so much of a problem. In fact, I don't believe I would use a form/subform for that.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    For 1-to-1, depends, one of the tables is probably 'dominant' - would not want to permit a record on one side when no record in the 'dominant'. Can be managed without subform using some other method to make sure the dominant record is created first. I have this situation.
    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.

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

Similar Threads

  1. Include Decimals when Adding Fields
    By janmack79 in forum Queries
    Replies: 3
    Last Post: 04-24-2017, 02:01 PM
  2. Replies: 1
    Last Post: 12-12-2016, 03:07 PM
  3. Replies: 1
    Last Post: 10-12-2012, 03:23 PM
  4. Include ROW Count in Query???
    By taimysho0 in forum Queries
    Replies: 21
    Last Post: 05-25-2012, 05:29 PM
  5. Include zero values in below query!
    By daffykyle in forum Access
    Replies: 3
    Last Post: 11-30-2011, 08:56 AM

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