访问 - 从组合框错误填充子表单

时间:2017-01-06 02:03:16

标签: vba ms-access access-vba

我对编码非常非常新,所以请放心吧!

我已经按照教程允许我从主窗体中的组合框填充子窗体。我只是想知道我可以改变什么以确保我不会一直收到这个错误。当一个模块被添加到学生时,我希望表单允许我退出它,如果我想要或继续不添加任何其他内容但是当我尝试离开表单时,我收到一条错误消息,让我选择另一个模块。我怎样才能阻止这种情况发生?

请参阅以下代码和屏幕截图:

Form appearance when I have selected a student and module ready to add

Error message once I have added a module and want to leave the form, I wouldnt want students to see this or have this difficulty

表格VBA代码,请注意我还有LinkMaster的子表单作为studentcombo,LinkChild作为StudentID

'combo box for StudentID updates all other student detail text boxes.
Private Sub studentcombo_AfterUpdate()
    programmetb = DLookup("ProgrammeID", "tblStudent", "[StudentID]=studentcombo")
    firstnametb = DLookup("FirstName", "tblStudent", "[StudentID]=studentcombo")
    surnametb = DLookup("Surname", "tblStudent", "[StudentID]=studentcombo")
    SNtb = DLookup("StudentNumber", "tblStudent", "[StudentID]=studentcombo")
    StudentIDhidden = studentcombo
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")
    ModuleCodehidden = modulecodecombo
End Sub

Private Sub AddModuleBut_Click()
    'Verification that studentID is selected.
    If IsNull(studentcombo) Then
        MsgBox "Please select student", , "Required"
        studentcombo.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
        StudentIDhidden = studentcombo
    End If
End Sub

提前致谢! ^^

1 个答案:

答案 0 :(得分:0)

验证表单是否包含空字段。那些空的必填字段不允许您退出。如果找到任何,则撤消表单中收集的所有数据。将以下代码放在一个按钮中。将其命名为退出按钮。

If Me.Dirty Then
        Me.Undo
        DoCmd.Close
End If