简体   繁体   中英

MS Access populate table based on subform entry

I have a class registration database for our organization. The major things it tracks are: user info (name etc), track info (ie your major ex: biology), and course info (what courses are for what track).

So the data structure looks like this.

tblUsers
UserID (PK)
FirstName
LastName
.
tblUserTrack
UserTrackID (PK)
UserID (FK) - links to tblUsers
TrackID (FK - links to tblTrack).
.
tblTrack
TrackID (PK)
TrackName
.
tblCourses
CourseID (PK)
CourseName
TrackID (FK links to tblTrack)
.
tblRegistrationNew
RegistrationID (PK)
UserTrackID (FK links to tblUserTrack)
Grade
CompletionDTTM

The registration form has 3 elements: The parent form has the user demographics The track subform has the user/track info, linked to the parent form by UserID. You can add tracks to the user without a problem (In our org they can have multiple tracks, for example biology and chemistry).

The goal is for the third form to populate the registration information based on what the user inputs. But whenever I add in the registration table to the query it does not update (which makes sense from a SQL point of view).

Is it possible to update/insert rows to the registration table when I update the track table? If so how? I have MS SQL Server and C#.NET experience so I'm not completely clueless (just mostly).

Also I found this link to a well done registration database but it does not have the feature I need. http://office.microsoft.com/en-gb/templates/classroom-management-database-TC001018407.aspx?CategoryID=CT101426031033&av=ZAC000&AxInstalled=1&c=0

Edit: getting reasonably close. I realized I can do a left join to the registration table but I can not update records in the form. The query is here:

SELECT tblUserTrack.UserTrackID, tblUserTrack.UserID, tblUserTrack.TrackID, tblCourses.[Course Titles], tblRegistrationNew.Grade, tblRegistrationNew.CompleteDTTM FROM (tblUserTrack INNER JOIN tblCourses ON tblUserTrack.TrackID = tblCourses.TrackID) LEFT JOIN tblRegistrationNew ON tblUserTrack.UserTrackID = tblRegistrationNew.UserTrackID;

So there wasn't really a good solution. I tried various modifications of the database design but what I have written above is already well designed. For my needs I had a single form with two subforms. The main form had student demographics such as name, address. The first subform was the track (basically the degree ie biology) in a dropdown, which populated the tblUserTrack table. I then put code in the AfterInsert event to generate new rows in the tblRegistration table when the user inserts new rows.

Private Sub Form_AfterInsert()

    Dim strSQL As String

    strSQL = "INSERT INTO tblRegistration (UserTrackID, CourseID) " & _
        "SELECT tblUserTrack.UserTrackID, tblCourses.CourseID FROM tblUserTrack INNER JOIN " & _
        "tblCourses ON tblCourses.TrackID = tblUserTrack.TrackID " & _
        "WHERE tblUserTrack.UserID = " & UserID.Value & "AND tblUserTrack.UserTrackID NOT IN " & _
        "(SELECT tblRegistration.UserTrackID FROM tblRegistration INNER JOIN tblUserTrack ON tblUserTrack.UserTrackID = tblRegistration.UserTrackID " & _
        "WHERE tblUserTrack.UserID = " & UserID.Value & ")"

    DoCmd.RunSQL (strSQL)


End Sub

After this it worked satisfactorily. The only thing I did not do is handle the situation when the track changed (ie from biology to chemistry), in this case I instructed the user just to delete the old track (biology) and insert a new track (chemistry). The long and short of it after many many hours of research is that there really isn't a way to do a 'cascade insert' which is what i needed to do, at least to the best of my knowledge. At least now I know what the code is doing in terms of the SQL execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM