简体   繁体   English

MS Access根据子窗体条目填充表

[英]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. 注册表单包含3个元素:父表单具有用户人口统计信息跟踪子表单具有用户/跟踪信息,并通过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). 但是,每当我将注册表添加到查询中时,它都不会更新(从SQL的角度来看这很有意义)。

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). 我有MS SQL Server和C#.NET的经验,所以我并非一无所知(只是大部分情况下)。

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 http://office.microsoft.com/zh-CN/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; 选择tblUserTrack.UserTrackID,tblUserTrack.UserID,tblUserTrack.TrackID,tblCourses。 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. 第一个子表单是下拉列表中的轨道(基本上是学位,即生物学程度),其中填充了tblUserTrack表。 I then put code in the AfterInsert event to generate new rows in the tblRegistration table when the user inserts new rows. 然后,我将代码放在AfterInsert事件中,以便在用户插入新行时在tblRegistration表中生成新行。

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. 至少现在我知道代码在SQL执行方面的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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