繁体   English   中英

访问,使用组合框填充子表单

[英]Access, populate subform with combobox

由于我是使用Access / VBA的初学者,我无法判断我的查询是否已经在另一个论坛中得到解决,所以如果有的话我会道歉。

我试图使用主窗体上的组合框中的值填充子窗体。 我希望当我在组合框中选择一个学生,然后在另一个组合框中选择一个跟随模块并单击“添加模块”时,它会将模块添加到子表单中,并在底部子表单中显示为该学生选择的所有模块。 我按照视频获取了我目前拥有的代码,下面显示了我当前布局的截图(借口设计功能尚未开始考虑)。

请注意:对于docmd,代码肯定是不正确的,我不确定要填充子窗体的内容,以便显示所选学生和所选的所有模块。

当您打开表单时,它看起来像这样,理想情况下我希望它为特定学生添加记录时的外观

单击“添加模块”后,它看起来如何进入新记录并且不显示完整列表,除非我单击箭头。

代码 -

'combo box for StudentID updates all other student detail text boxes.
Private Sub studentIDcombo_AfterUpdate()
    programmetb = DLookup("ProgrammeID", "tblStudent", "[StudentID]=studentidcombo")
    firstnametb = DLookup("FirstName", "tblStudent", "[StudentID]=studentidcombo")
    surnametb = DLookup("Surname", "tblStudent", "[StudentID]=studentidcombo")
    StudentID1 = studentIDcombo
End Sub

'combo box for ModuleCode updates all other module detail text boxes.
Private Sub modulecodecombo_AfterUpdate()
    modulenametb = DLookup("ModuleName", "tblModule", "[ModuleCode]=modulecodecombo")
    creditstb = DLookup("Credits", "tblModule", "[ModuleCode]=modulecodecombo")
    semester1tb = DLookup("Semester_1", "tblModule", "[ModuleCode]=modulecodecombo")
    semester2tb = DLookup("Semester_2", "tblModule", "[ModuleCode]=modulecodecombo")
    prereqtb = DLookup("Pre_requisites", "tblModule", "[ModuleCode]=modulecodecombo")
End Sub

Private Sub AddModuleBut_Click()
    'Verification that studentID is selected.
    If IsNull(studentIDcombo) Then
        MsgBox "Please select student", , "Required"
        studentIDcombo.SetFocus
        Exit Sub
    End If
    'Verification that modulecode is selected.
    If IsNull(modulecodecombo) Then
        MsgBox "Please select a course", , "Required"
        modulecodecombo.SetFocus
        Exit Sub
    'Else create a record in the subform using the combo box data.
    Else
        DoCmd.GoToRecord , , acNewRec
        StudentID1 = studentIDcombo
        modulecodecombo.SetFocus
    End If
End Sub

额外的信息:

在我能够为特定学生添加模块之后,我将开始编码条件,例如,如果您之前已经完成模块B等,则只能选择模块A.等。这可以通过当前布局实现吗?

我将制作一个学生表格来添加学生,然后他们将在此表格上添加模块。

在此先感谢任何帮助,我希望这是有道理的!

最诚挚的问候,<3

首先,考虑重新设计表以使用正确的主键(自动增量整数)。 说过你缺少的是创建新学生模块记录的命令。 它看起来像这样:

DoCmd.RunSQL "INSERT INTO [studentmodulelinktable] (ProgrammeID,ModuleName) VALUES (" & studentIDcombo & ",'" & modulecodecombo & "')"

以上假设studentIDcombo具有数值,而modulecodecombo具有文本值。 区别在于您是否使用单引号。

暂无
暂无

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

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