简体   繁体   English

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

[英]Access, populate subform with combobox

As I am such a beginner at using Access/VBA I couldn't tell if my query had already been addressed in another forum, so I apologise if it had. 由于我是使用Access / VBA的初学者,我无法判断我的查询是否已经在另一个论坛中得到解决,所以如果有的话我会道歉。

I am trying to populate a subform using the values from the combo boxes on the main form. 我试图使用主窗体上的组合框中的值填充子窗体。 I am hoping that when I select a student in the combo box and then a following module in the other combobox and click 'Add Module' it will add the module to the subform and show all modules chosen for that student in the bottom subform. 我希望当我在组合框中选择一个学生,然后在另一个组合框中选择一个跟随模块并单击“添加模块”时,它会将模块添加到子表单中,并在底部子表单中显示为该学生选择的所有模块。 I followed a video to obtain the code I currently have and this is displayed below along with a screenshot of my current layout (excuse design features these haven't even begun to be considered yet). 我按照视频获取了我目前拥有的代码,下面显示了我当前布局的截图(借口设计功能尚未开始考虑)。

Please note: The code is definitely incorrect for the docmd, I am unsure what to use in order to populate the subform so it will display the selected student and all modules chosen. 请注意:对于docmd,代码肯定是不正确的,我不确定要填充子窗体的内容,以便显示所选学生和所选的所有模块。

When you open form it looks like this, ideally how I want it to look when records are added for specific students 当您打开表单时,它看起来像这样,理想情况下我希望它为特定学生添加记录时的外观

How it looks after I click 'Add module' goes to new record and doesn't display full list unless I click back on arrows. 单击“添加模块”后,它看起来如何进入新记录并且不显示完整列表,除非我单击箭头。

Code - 代码 -

'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

Extra information: 额外的信息:

After I am able to add modules for specific students I will begin coding conditions, such as you can only choose module A if you have done module B previously etc etc. Will this be possible to achieve with this current layout? 在我能够为特定学生添加模块之后,我将开始编码条件,例如,如果您之前已经完成模块B等,则只能选择模块A.等。这可以通过当前布局实现吗?

I will be making a student form to add students to and then they will add modules on this form. 我将制作一个学生表格来添加学生,然后他们将在此表格上添加模块。

Thanks in advance for any help and I hope this makes sense! 在此先感谢任何帮助,我希望这是有道理的!

Best Regards, <3 最诚挚的问候,<3

First of all, consider re-designing your tables to use proper primary keys (auto-increment integers). 首先,考虑重新设计表以使用正确的主键(自动增量整数)。 Having said that what you are missing is the command to create the new student-module record. 说过你缺少的是创建新学生模块记录的命令。 It would look something like this: 它看起来像这样:

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

The above assumes studentIDcombo has numerical values and modulecodecombo has text values. 以上假设studentIDcombo具有数值,而modulecodecombo具有文本值。 The difference is whether you use the single quotes or not. 区别在于您是否使用单引号。

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

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