【问题标题】:Access - populate subform from combo box error访问 - 从组合框错误填充子表单
【发布时间】:2017-01-06 02:03:16
【问题描述】:

我对编码非常非常陌生,所以请放轻松!

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

请看下面的代码和截图:

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

提前致谢! ^^

【问题讨论】:

    标签: ms-access vba


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!有没有办法可以将其编码到我的添加模块按钮中,以便在添加模块时自动清空模块详细信息?如果不是,这仍然是可行的解决方案!
    • 有没有办法完全避免这个错误?如果选择了一个学生,然后您尝试做任何事情,而不是选择一个模块并添加它,错误仍然会弹出,看起来不专业。我可以尝试完全避免这种情况吗?
    • 您要么处理异常,要么删除这些字段的必需属性。通过选择后者,您将不得不编写更多代码以验证数据是否存在。
    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多