Userform QueryClose只能工作一次

时间:2018-02-23 20:22:29

标签: excel vba excel-vba

我当前的问题是,一旦通过点击右上角的X按钮运行查询关闭并调用我设置的参数并再次尝试再次执行,则禁用X按钮。

我有一个使用下面显示的查询关闭代码的用户表单。

msgSetRequest = qbSessionManager.CreateMsgSetRequest("US", 13, 0)

表单此调用询问用户是否要保存更改并请求"是"或"否"设置2个按钮没有问题,我已在该窗体上设置QueryClose(通过在Userform上按下X按钮) 将您发送回主用户表单。

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
    MainMenuUserform.Hide
    CloseForm.Show
    End If
End Sub

这是表格

enter image description here

我遇到的问题是,一旦运行了Queryclose,它就不会再次调用它。禁用主用户窗口上的X按钮。以及关闭表格

我正在使用[userform.Hide],因为当我使用[Unload Userform]命令时会调用该userform再次显示它会给我" Userform已经显示"错误。

也许我正在做一个简单的任务太难了。

我将非常感谢帮助和反馈。谢谢,

主要表格

Main Userform

2 个答案:

答案 0 :(得分:1)

它只运行一次,因为一旦CloseForm.Show返回,处理程序退出并且对象被销毁,因为你从未取消结束:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        Cancel = True
        Me.Hide
        CloseForm.Show
    End If
End Sub

注意我已将MainMenuUserform替换为MeThis article of mine深入探讨了推理。简而言之,您指的是表单的默认实例,这不可避免地会导致问题。

FWIW CloseForm很可能是一个简单的MsgBox,给定vbYesNo按钮:这样就不需要让表单互相交互。

如果您愿意做正确的事情,那么您将要实现一个正确的对话框界面 - 这将是该/是形式的代码隐藏:

Option Explicit
Private mDialogResult As VbMsgBoxResult

Public Property Get DialogResult() As VbMsgBoxResult
    DialogResult = mDialogResult
End Property

Private Sub YesButton_Click()
    mDialogResult = vbYes
    Me.Hide
End Sub

Private Sub NoButton_Click()
    mDialogResult = vbNo
    Me.Hide
End Sub    

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        Cancel = True
        Me.Hide
    End If
End Sub

请注意,它不以任何方式,形状或形式与其他形式相结合;它既不知道要保存什么,也不知道如何处理对话结果。它的工作是向用户提示消息,并将结果返回给调用者。

这将是MenuMenuUserForm代码隐藏中的逻辑:

Option Explicit
Private mCancelled As Boolean

Public Property Get IsCancelled() As Boolean
    IsCancelled = mCancelled
End Propety

Private Sub PromptToSaveChanges()
    With New CloseForm
        .Show
        mCancelled = (.DialogResult = vbNo)
    End With
End Sub

Private Sub OkButton_Click()
    Me.Hide
End Sub

Private Sub CancelButton_Click()
    PromptToSaveChanges
    Me.Hide
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        Cancel = True
        PromptToSaveChanges
        Me.Hide
    End If
End Sub

现在,我没有完整的背景,所以我做出了这些假设:

  • 主窗体有一个[确定]按钮,不会提示保存任何更改。如果使用该按钮关闭表单,则会保存更改。
  • 主窗体有一个[取消]按钮,与" x-out out"完全相同。形式;提示用户,并相应地保存或丢弃更改。
  • 提示结果为vbYesvbNo;用户没有机会退出并取消此时的结束(即返回vbCancel并跳过Me.Hide

所以调用代码看起来像这样:

With New MainMenuUserForm
    .Show
    If Not .IsCancelled Then
        ' save changes... whatever that means
    End If
End With 

请注意,在任何时候都不会显示任何表单的默认实例:它始终是New个实例。这就是确保在全局范围内没有状态丢失,并且被自毁对象意外丢弃的原因。

答案 1 :(得分:0)

official documentation所示,您需要取消QueryClose事件。

也许您还要使用Me而不是名称,并Hide / Show按正确的顺序使用。

' In MainMenuUserform
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = 1
        Me.Hide
        CloseForm.Show
    End If
End Sub

' In CloseForm
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Me.Hide
        MainMenuUserform.Show
        Me.Unload
    End If
End Sub

可能不需要If CloseMode = vbFormControlMenu Then ...

我在Ubuntu下,所以我无法测试它。