访问VB嵌套如果那么多

时间:2015-10-16 21:43:38

标签: ms-access if-statement access-vba

我在Access工作并且遇到了if语句问题。它永远不会得到更新,它总是到最后一个。如果我尝试清除下一个条目的字段,它确实无法工作。有人可以提供一些见解吗?感谢。

'Check to see they entered a skill rating - this works well
If IsNull(Me.Skill_Combo.Value) Then
    MsgBox "Please enter skill rating.", vbCritical + vbOKOnly, "Error"
ElseIf IsNull(Me.Attitude_Combo.Value) Then
    MsgBox "Please enter an attitude rating.", vbCritical + vbOKOnly, "Error"
ElseIf IsNull(Me.Listens_Combo.Value) Then
    MsgBox "Please enter an Listens/Follows Direction rating.", vbCritical + vbOKOnly, "Error"
ElseIf IsNull(Me.Punctual_Combo.Value) Then
    MsgBox "Please enter an Punctual rating.", vbCritical + vbOKOnly, "Error"
'Check to see if the rating is 0 or 1, and if so require them to leave a comment
ElseIf (Me.Skill_Combo.Value < 2 Or Attitude_Combo.Value < 2 Or Listens_Combo.Value < 2 Or Punctual_Combo.Value < 2) Then
    If IsNull(Comment_Box.Value) Then
        MsgBox "Please enter a comment for ratings lower than 2.", vbCritical + vbOKOnly, "Error"  'It gets here like it should, but only the first time
    Else  'It never gets here!
        DoCmd.OpenQuery "UpdateEmployee", acViewNormal, acEdit
        MsgBox "Successfully Updated" & ".", vbOKOnly, "Success"
        'Clear values to select next employee
'                Me.Skill_Combo.Value = ""
'                Me.Attitude_Combo.Value = ""
'                Me.Listens_Combo.Value = ""
'                Me.Punctual_Combo.Value = ""
'                Me.Comment_Box.Value = ""
    End If
Else
        MsgBox "You shouldn't be getting this message box, but it always 

ends up here"

我也试过这个并且效果更好,但仍然不起作用..

'Check to see if the rating is low, and if so require a comment
ElseIf (Me.Skill_Combo.Value < 2 Or Attitude_Combo.Value < 2 Or Listens_Combo.Value < 2 Or Punctual_Combo.Value < 2) And IsNull(Comment_Box.Value) Then
        MsgBox "Please enter a comment for ratings lower than 2.", vbCritical + vbOKOnly, "Error"
ElseIf (Me.Skill_Combo.Value >= 2 Or Attitude_Combo.Value >= 2 Or Listens_Combo.Value >= 2 Or Punctual_Combo.Value >= 2) And Not IsNull(Comment_Box.Value) Then

2 个答案:

答案 0 :(得分:1)

最有可能的是,你的组合没有Null值。

预插入和/或插入调试行以查找,例如:

Debug.Print Me!Skill_Combo.Name, Me!Skill_Combo.Value

或测试空字符串而不是Null:

If Nz(Me!Skill_Combo.Value) = "" Then

答案 1 :(得分:0)

这似乎有效。谢谢你带领我朝着正确的方向前进!

'Check to see they entered a skill rating
If (Me.Skill_Combo.Value = "") Or IsNull(Me.Skill_Combo.Value) Then
    MsgBox "Please enter skill rating.", vbCritical + vbOKOnly, "Error"
ElseIf (Me.Attitude_Combo.Value = "") Or IsNull(Me.Attitude_Combo.Value) Then
    MsgBox "Please enter an attitude rating.", vbCritical + vbOKOnly, "Error"
ElseIf (Me.Listens_Combo.Value = "") Or IsNull(Me.Listens_Combo.Value) Then
    MsgBox "Please enter an Listens/Follows Direction rating.", vbCritical + vbOKOnly, "Error"
ElseIf (Me.Punctual_Combo.Value = "") Or IsNull(Me.Punctual_Combo.Value) Then
    MsgBox "Please enter an Punctual rating.", vbCritical + vbOKOnly, "Error"

'Check to see if the rating is low, and if so require a comment
ElseIf (Me.Skill_Combo.Value < 2 Or Attitude_Combo.Value < 2 Or Listens_Combo.Value < 2 Or Punctual_Combo.Value < 2) And ((IsNull(Me.Comment_Box.Value)) Or (Me.Comment_Box.Value = "")) Then
        MsgBox "Please enter a comment for ratings lower than 2.", vbCritical + vbOKOnly, "Error"
        DoCmd.Requery "EmpList"
  Else
        DoCmd.SetWarnings False
        DoCmd.OpenQuery "UpdateEmployee", acViewNormal, acEdit
        DoCmd.SetWarnings True
        MsgBox "Successfully Updated" & ".", vbOKOnly, "Success"

        Me.Skill_Combo.Value = ""
        Me.Attitude_Combo.Value = ""
        Me.Listens_Combo.Value = ""
        Me.Punctual_Combo.Value = ""
        Me.Comment_Box.Value = ""
        DoCmd.Requery "EmpList"


End If
End Sub