我有一个包含14个字段的表单,其中总共需要10个字段,其中有3个(Other Absence Type
,Illness Type
和Other Illness Type
)是有条件的。表单上还有一个“保存记录”按钮,其中有一个子程序,用于检查在保存记录之前是否已填写所有必填字段。十个必填字段的Required属性在表中设置为Yes
。
启用/禁用三个条件必需字段的代码似乎独立工作,并且用于检查是否所有必填字段都已填充的子例程似乎也可以单独工作。但是,只要启用了其中一个有条件要求的字段,但也留空,我尝试保存记录,表单会冻结,我无法再进行更改。
Private Sub Form_Load()
With Me
.FirstName.SetFocus
.Other_Absence_type.Enabled = False
.Illness_type.Enabled = False
.Other_Illness_type.Enabled = False
End With
End Sub
Private Sub Absence_type_Click()
If [Absence type] = "Staff illness" Then
[Illness type].Enabled = True
Else
Me.[Illness type].Value = ""
Me.[Illness type].Enabled = False
Me.[Other Illness Type].Value = ""
Me.[Other Illness Type].Enabled = False
End If
If Me.[Absence type] = "Other (Please specify)" Then
Me.[Other Absence Type].Enabled = True
Me.Other_Absence_type.SetFocus
Else
Me.[Other Absence Type].Value = ""
Me.[Other Absence Type].Enabled = False
End If
End Sub
Private Sub Illness_type_Click()
If Me.[Illness type] = "Other (Please specify)" Then
Me.[Other Illness Type].Enabled = True
Me.Other_Illness_type.SetFocus
Else
Me.[Other Illness Type].Value = ""
Me.[Other Illness Type].Enabled = False
End If
End Sub
Private Sub SaveRecordNew_Click()
On Error GoTo ErrorHandler
DoCmd.RunCommand acCmdSaveRecord
ErrorHandler:
MsgBox "One or more required fields are blank", vbExclamation
答案 0 :(得分:1)
假设validate()
是检查每个字段是否已填充的函数,您需要在表单的before_update事件中调用它。这是功能:
Function validate() as Boolean
Dim ret as Boolean
If (textbox1.Value & "") = "" or (textbox2.Value & "") = "" or ... Then
ret = false
Else
ret = true
End If
validate = ret
End Function
Before_Update表单事件:
Private sub Form_BeforeUpdate(cancel as integer)
If not validate() then
MsgBox "Fill all required fields, please"
cancel = true
End If
End Sub