在先前已执行VBA时,访问VBA无效

时间:2018-02-03 02:46:26

标签: vba checkbox textbox

我在VBA很新。我有一个带有文本框和复选框的表单。如果我打开表单并单击复选框而不填充文本框,我会收到一个消息框以输入注释。

但是,在同一个会话中,如果我取消选中该框并重新检查它,即使文本框为空,我也不会在消息框中输入注释。

我如何才能获得代码" refire"在同一个会话中给我输入评论的消息框?

以下是我的代码:

Private Sub Check29_Click()

    If IsNull(Me.Text16) Then

        MsgBox "Comments are Required.", vbCritical
        Me.Check29 = Null
        Exit Sub

    Else

        If Me.Check29 = -1 Then

            Dim RS As DAO.Recordset
            Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
            RS.AddNew
            RS("HW535ID") = Me![HWID]
            RS("Excluded") = "Yes"
            RS("BOA Assignee") = Me![AssignedBA]
            RS("Comments") = Me![Text16]
            RS("CheckBox") = Me![Check29]
            RS("Date of Exclusion") = Me![Text115]
            RS("ReviewID") = Me![Text33]
            RS.Update
            RS.Close
            Set RS = Nothing
            Exit Sub

        Else

            DoCmd.SetWarnings False
            DoCmd.OpenQuery ("RemoveExclusion")
            Me.Text16 = Null
            Exit Sub

        End If

    End If

End Sub

2 个答案:

答案 0 :(得分:0)

我可以在代码中看到逻辑问题。请注意这一行:

  Me.Check29 = Null

重新触发 _click事件并产生意外结果。它也是不正确的(应该是Me.Check29.Value = False)。请尝试以下修订版:

声明模块级变量

 Option Explicit
 Private bCancel as Boolean

事件代码(我也做了更多更正):

Private Sub Check29_Click()

if bCancel Then Exit Sub
bCancel = False

If Trim(Me.Text16.Text) = vbnullstring Then

    MsgBox "Comments are Required.", vbCritical
    bCancel = True
    Me.Check29.Value = False
    bCancel =  False
    Exit Sub

Else

    If Me.Check29.Value = True Then

        Dim RS As DAO.Recordset
        Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
        RS.AddNew
        RS("HW535ID") = Me![HWID]
        RS("Excluded") = "Yes"
        RS("BOA Assignee") = Me![AssignedBA]
        RS("Comments") = Me![Text16]
        RS("CheckBox") = Me![Check29]
        RS("Date of Exclusion") = Me![Text115]
        RS("ReviewID") = Me![Text33]
        RS.Update
        RS.Close
        Set RS = Nothing
        Exit Sub

    Else

        DoCmd.SetWarnings False
        DoCmd.OpenQuery ("RemoveExclusion")
        Me.Text16.Text = vbnullstring
        Exit Sub

    End If

End If

End Sub

答案 1 :(得分:0)

Private Sub Check29_Click()

如果bCancel则退出Sub bCancel = False

Me.Text16.SetFocus

如果修剪(Me.Text16.Text)= vbNullString而Me.Check29.Value = True则

MsgBox "Comments are Required.", vbCritical
bCancel = True
Me.Check29.Value = False
bCancel = False
Exit Sub

否则

If Me.Check29.Value = True Then

    Dim RS As DAO.Recordset
    Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
    RS.AddNew
    RS("HW535ID") = Me![HWID]
    RS("Excluded") = "Yes"
    RS("BOA Assignee") = Me![AssignedBA]
    RS("Comments") = Me![Text16]
    RS("CheckBox") = Me![Check29]
    RS("Date of Exclusion") = Me![Text115]
    RS("ReviewID") = Me![Text33]
    RS.Update
    RS.Close
    Set RS = Nothing
    Exit Sub

Else
    If Me.Check29.Value = False Then

        DoCmd.SetWarnings False
        DoCmd.OpenQuery ("RemoveExclusion")
        Me.Text16 = ""
    Exit Sub

    End If

End If

结束如果

End Sub