示例:对象验证

此示例演示如何通过使用 CanPost 事件过程来执行所有字段的验证。例如,此代码段确保用户不能在没有添加注释的情况下拒绝缺陷。

在此示例中,用户不能发布缺陷状态 (BG_STATUS) 已更改为 Rejected 的缺陷,除非已在研发注释字段 (BG_DEV_COMMENTS) 中输入了一些解释文本。

将代码添加到 Bug_CanPost 事件过程,这样用户试图提交缺陷时将执行检查。

Function Bug_CanPost
        ' Initialize the function's return value
	 ' to avoid unpredictable behavior.
        Bug_CanPost =  False 
        On Error Resume Next
        If Bug_Fields("BG_STATUS").IsModified and _
        Bug_Fields("BG_STATUS").Value = "Rejected" and _
        not Bug_Fields("BG_DEV_COMMENTS").IsModified then
            Bug_CanPost = False
            msgbox "You must enter a comment when rejecting a defect."
        Else
            Bug_CanPost = True
        End If
        PrintError "Bug_CanPost"
        On Error GoTo 0
End Function