2017-01-06 74 views
0

我對編碼非常非常新,所以請放輕鬆!訪問 - 從組合框錯誤填充子窗體

我跟着教程,讓我從主窗體組合框填充子窗體。我只是想知道我可以改變什麼,以確保我不會收到這個錯誤。當一個模塊被添加到學生時,我希望表單允許我退出它,如果我想要或者繼續不添加任何其他內容,但是當我嘗試離開表單時,我會收到一條錯誤消息,讓我選擇另一個模塊。我怎樣才能阻止這種情況發生?

請參見下面的代碼和截圖:

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 

提前致謝! ^^

回答

0

驗證表單是否有空字段。那些空的必填字段不會讓你退出。如果發現任何問題,請撤消表單中收集的所有數據。將下面的代碼放在一個按鈕中。將其命名爲退出按鈕。

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

非常感謝!有沒有一種方法可以將它編碼到我的添加模塊按鈕中,以便模塊細節在添加時自動清空? 如果不是這仍然是可行的解決方案! – User666111

+0

有沒有辦法可以完全避免這個錯誤?如果選擇了學生,然後嘗試除選擇模塊之外的任何其他內容並添加它,錯誤仍然會彈出,這看起來不專業。我可以儘量避免這種情況嗎? – User666111

+0

您要麼處理異常,要麼刪除這些字段的必需屬性。通過選擇後者,您將不得不編碼更多以驗證數據是否存在。 – Lybren