VBA如果陈述混乱

时间:2016-08-25 09:57:14

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

下面的代码工作正常,直到我添加了vbokcancel位代码...下面的代码:

If Me.results.ListIndex = -1 Or Me.userfolder.ListIndex = -1 Or
Me.choice.ListIndex = -1 Then           

MsgBox "Please choose something to remove"

Else

If MsgBox("Are you sure you want to delete this user from this folder?", vbOKCancel) = vbOK Then

    If Me.choice.Value = "Folder" Then

        username = Me.results.Column(0)
        prfolder = Me.results.Column(3)

        strSQL = "DELETE tblRelationship.*, tblPra.praNo, tblFolder.folder FROM tblPra INNER JOIN (tblFolder INNER JOIN tblRelationship ON tblFolder.folderID = tblRelationship.folderID) ON tblPra.praID = tblRelationship.praID WHERE (((tblPra.praNo)='" & username & "') AND ((tblFolder.folder)='" & prfolder & "'));"
        CurrentDb.Execute strSQL
        Me.results.Requery

    ElseIf Me.choice.Value = "Username" Then

        prfolder = Me.results.Column(0)

        strSQL = "DELETE tblRelationship.*, tblFolder.folder FROM tblPra INNER JOIN (tblFolder INNER JOIN tblRelationship ON tblFolder.folderID = tblRelationship.folderID) ON tblPra.praID = tblRelationship.praID WHERE (((tblFolder.folder)='" & prfolder & "'));"
        CurrentDb.Execute strSQL
        Me.results.Requery

    End If
  End If
End If

它将激活If MsgBox("Are you sure you want to delete this user from this folder?", vbOKCancel) = vbOK部分但是如果" OK"单击按钮,它会直接跳到End If,并且不会在IF语句中运行代码。现在我觉得我做的很完美"法律"在编码中,显然不是......任何想法,我已经做了什么搞砸了?

如果按"取消"它完成了我已经想要它做的事情,它关闭了消息框并且用户回到了同一个屏幕..因此它不需要Else语句,因为它已经完成了我想要它做的事情。

1 个答案:

答案 0 :(得分:1)

这对于评论来说太长了,而且它不是一个真正的答案,但它应该引导你朝着正确的方向发展。

尝试将消息框的值设置为等于变量。像这样:

If Me.results.ListIndex = -1 Or Me.userfolder.ListIndex = -1 Or
Me.choice.ListIndex = -1 Then           

MsgBox "Please choose something to remove"

Else

    X = MsgBox("Are you sure you want to delete this user from this folder?", vbOKCancel)

    If X = vbOK Then  'put a breakpoint here

        If Me.choice.Value = "Folder" Then

            username = Me.results.Column(0)
            prfolder = Me.results.Column(3)

            strSQL = "DELETE tblRelationship.*, tblPra.praNo, tblFolder.folder FROM tblPra INNER JOIN (tblFolder INNER JOIN tblRelationship ON tblFolder.folderID = tblRelationship.folderID) ON tblPra.praID = tblRelationship.praID WHERE (((tblPra.praNo)='" & username & "') AND ((tblFolder.folder)='" & prfolder & "'));"
            CurrentDb.Execute strSQL
            Me.results.Requery

        ElseIf Me.choice.Value = "Username" Then

            prfolder = Me.results.Column(0)

            strSQL = "DELETE tblRelationship.*, tblFolder.folder FROM tblPra INNER JOIN (tblFolder INNER JOIN tblRelationship ON tblFolder.folderID = tblRelationship.folderID) ON tblPra.praID = tblRelationship.praID WHERE (((tblFolder.folder)='" & prfolder & "'));"
            CurrentDb.Execute strSQL
            Me.results.Requery

        End If
    End If

End If

然后,检查X的值,看看它认为X等于什么。这应该可以让您了解为什么If / Then代码块不起作用。