Complicated database form

Hi. I have a complicated (for me!) database and form. Below is a screenshot of relationships

I have a form (form2-By Subject) based on tbl-Subjects (grid). This has a subform grid based on tbl-Tutorials and there is a further subform based on a qry.

I click on subject and it shows tutorials in grid. Click on tutorial and it shows details.
All works well except for 1 record. Records are correct in tables but when clicking subject it will not show the tutorial. All others are fine. This record is correct in tables.

Difficult to explain so I have uploaded database so you can see what I have done - and hopefully find what I’ve done wrong!!

Download database: https://123club.co.uk/tutorials.zip

Which one?

@keme1 3D in the subject. It should be showing Tutorial ID 5 (Photoshop Fun in 3D),

Only a look on the relationships:
“tbl-Tutorials” should be the main form. It is linked 1:n to subform “tbl-Subjects”. “tbl-Subject_List” is content of a listbox in “tbl-Subjects”. Don’t know why you created this table “tbl-Subject_List”, which only contains strings, which will also be saved in “tbl-Subjects”, but this is the construction you will need.

“tbl-Tutorials” will contain 3 Listboxes for “tbl-Tutors”, “tbl-Artists” and “tbl-Curses”.

@RobertG Thank you. Must admit I’m a bit confused.

Yes, I do have a main form for tutorials - no problem with that.

However, I also have a form for subjects so that I can see a list of all subjects then click on one and see a list of tutorials for that subject.

I have a “tbl-Subject_List” as I need a ‘look-up’ table for the “tbl-Subjects”

I don’t see how your suggested construction will work or help me.

If you have the time to download and look at my database, you might understand my needs and the actual problem I am having.

Tutorials - Copy.odb (83.9 KB)

The form wizard does not even cover the essentials. For instance, the wizard never creates any list boxes.
I changed the primary key of the subjects and the subjects list. See relations window.

My form includes data from all tables in editable subforms.

1 Like

@Villeroy Thank you so so much - it’s working!!
Now just got to correct the broken bits in the main Tutorials form :slight_smile:

Just drop them and create them newly from scratch. In my form, the main form and any of its subforms is linked to a single table. Foreign keys are represented by listboxes. Related record sets are shown in one subform and 2 sub-subforms linked by their related pairs of primary/forein keys.
You may start with the wizard to create a first pair of main form and sub-form. But then you have to continue with the form navigator (menu:Form>“Form Navigator”).
Replace numeric foreign keys to listboxes (right-click>Replace with>Listbox), bind a listbox query to the listbox and set the drop-down property which unfortunately is not set for stand-alone listboxes.

Before creating new forms:
There is also “tbl-Courses” which uses the course name as primary and foreign keys in the same way as you used the subject names.
Demo of problem: Open either “tbl-Courses” or the related tutorials table and try to edit any name that occurs in both tables, for instance “Shift Art” to “Shifting Arts”. It is impossible to change this piece of meaningul user data because the name is copied as a foreign key in tbl-Tutorials. This relation enforces foreign key values that do exist as a primary key in the tbl-Courses.
Simple solution: Double-click the line in the relations window and select option “Update cascade”. Now you can change the primary key in tbl-Courses and the foreign keys in tbl-Tutorials will follow. Option “Delete cascade” deletes all related records in tbl-Tutorials when you delete one record in tbl-Courses.
My personal preference is a name column together with an auto-ID (may be because I’m simply used to it).
How to implement a CourseName with auto-ID, keeping the existing table data (would be much easier if you simply drop the table with its data):

  1. Delete tbl-Courses from the relations window. This will also delete the relation.
  2. Rename the table, say “tbl-Courses-old” (right-click table icon>Rename). It keeps the names.
  3. Create a new tbl-Courses with name and auto-ID as I did with tbl-Subject_List.
  4. Append an integer CoursesID to the tutorials, like my SID in the sources table.
  5. Call menu:Tools>SQL… and run
    INSERT INTO "tbl-Courses" (SELECT "CourseName",NULL FROM "tbl-Courses-old").
    If your auto-ID is the first column:
    INSERT INTO "tbl-Courses" (SELECT NULL, "CourseName" FROM "tbl-Courses-old")
    This writes the names from the old table into the new one and NULL to the auto-ID, which will be substituted with a unique auto-ID.
  6. Run the following statement to fill up the foreign key with the correponding IDs from the new table:
    UPDATE "tbl-Tutorials" SET "CourseID" =(SELECT "ID" FROM "tbl-Courses" WHERE "tbl-Tutorials"."CourseName"="tbl-Courses"."CourseName")
  7. Call menu:View>Refresh Tables
  8. Add the new “tbl-Courses” to the relations window and connect tbl-Tutorials.CoursesID with tbl-Courses.ID
  9. Open “tbl-Tutorials” for editing, make the CoursesID mandatory (Entry required = Yes) and delete the old “CourseName” column which is no longer referenced nor needed. (btw: I forgot to make tbl-Subjects.SID mandatory).
  10. Drop the old table.

With a meaningless auto-ID, you can edit all the user data without disturbing the relations established by the ID values.
If you want to enforce unique names (prevent duplicates) besides the unique auto-ID:
Open “tbl-Courses” for editing, menu:Tools>Index Design and create a unique index on the name column. This fails if the column has duplicates already.

P.S. The table editor fails with step 9. This works:

ALTER TABLE "tbl-Tutorials" ALTER COLUMN "CoursesID" SET NOT NULL;
ALTER TABLE "tbl-Tutorials" DROP COLUMN "CourseName";