VBA:使用多个用户表单

VBA: Using Multiple User Forms

我在使用多个用户表单时遇到问题。我知道这与传输变量有关,但我似乎做对了。我希望此代码首先询问用户他们是经理还是员工,然后如果他们选择经理,则要求他们在框中输入他们的经理 ID #,然后一旦发生这种情况,就会为经理弹出另一个用户表单。如果他们 select 员工,则会显示不同的用户表单。这是我到目前为止所拥有的。有人可以告诉我这里出了什么问题吗?

这是询问用户是经理还是员工的用户表单代码:

Private cancel As Boolean

Public Function ShowfrmType()
Public employeeType As String


'Show manager user form
If optManager.Value Then employeeType = "manager"


If optEmployee.Value Then employeeType = "employee"


ShowfrmType = Not cancel
Unload Me

End Function

模块代码如下:

Sub editEvaluation()


'Shows employeeType user form
frmType.Show

'if manager then enter manager ID #
'then allows manager to enter in an overall score for emplpoyee
If employeeType = "manager" Then
frmManager.Show
End if 

If employeeType = "employee" Then
frmEmployee.Show
MsgBox "Please fill out your self evaluation under the column: Self     Evalauation Score"


End If

我认为您在这里寻找的是无模式窗体。无模式对话框使您可以在对话框和另一个窗体之间转移焦点,而无需关闭对话框。显示对话框时,您可以继续在当前应用程序的其他地方工作。因此,例如:

Private cancel As Boolean

Public Function ShowfrmType()
    Public employeeType As String
    If optManager.Value Then employeeType = "manager"
    If optEmployee.Value Then employeeType = "employee"
    ShowfrmType = Not cancel
    Unload Me
End Function

Sub editEvaluation()
    frmType.Show vbModeless
    If employeeType = "manager" Then
        frmManager.Show vbModeless
    End If
    If employeeType = "employee" Then
        frmEmployee.Show vbModeless
        MsgBox "Please fill out your self evaluation under the column: Self     Evalauation Score"
    End If
End Sub

希望对您有所帮助。

我相信你可以继续使用 "modal" 用户表单(不要失去对发生的事情的控制)并且:

  • 让它们根据用户选择级联打开

  • 每次打开 "child" 用户表单时,隐藏当前 ("parent") 一个

  • 每次离开用户表单时

    • 有自己的代码只是隐藏它,而不是关闭或卸载它,因此它的 "parent" 用户窗体仍然可以访问它收集的数据

    • 在利用其收集的数据

    • 后,离开它的"parent"用户卸载它的任务

所以你可能有三个用户表单

  1. frmType

    这是 "main" 一个,由 "main" Sub editEvaluation()

    调用

    它只需要 OptionButtons(称为 "optManager" 和 "optEmployee")和以下代码

Private Sub optManager_Click() Me.Hide '<--| hide current (frmType) form frmManager.Show '<--| show frmManager to allow manager enter its data Unload frmManager '<--| have current ("parent") userform unload its "child" userforms End Sub

Private Sub optEmployee_Click() Me.Hide '<--| hide current (frmType) form frmEmployee.Show '<--| show employee userform to allow employee enter its data Unload frmEmployee '<--| have current ("parent") userform unload its "child" userforms End Sub

  1. frmManager

    它是 "manager" 一个,由 "main" frmType 用户表单

    调用

    在允许经理编辑其字段之前,它将首先询问他的 ID,对其进行验证,最后,如果验证成功结束,则授予对其字段的访问权限。

    否则它会隐藏自己

    要执行上面解释的经理 ID 询问和验证例程,您可以利用显示用户表单后立即触发的 UserForm_Activate() 事件

    所以将此代码添加到 frmManager 代码窗格

Private Sub UserForm_Activate() If Not CheckManagerID(InputBox("Please input your ID", "Manager ID", "AA000")) Then MsgBox "Sorry, your managerID is not Valid", vbCritical '<--| if managerID is NOT valid then inform the user and exit userform Me.Hide '<--| only hide the userform and let parent userform take care of unloading it Else MsgBox "Welcome!" & vbCrLf & "Now enter in your overall score for employee" '<--| if managerID IS valid then introduce him to the userform and let it shown to have the user (manager) fill its controls End If End Sub

Private Function CheckManagerID(ID As String) As Boolean CheckManagerID = ID Like "[A-Z][A-Z]###" ' check if passed ID has two capital letters followed by three digits End Function

(注意:CheckManagerID()函数只是字符串验证的一个例子)

以及您将放置所有需要的控件以收集 "validated" 经理

输入的数据的位置

如果你有一个 "Close" 按钮(假设你称它为 BtnClose),那么它的 Click 事件处理程序将是:

Private Sub BtnClose_Click() Me.Hide '<--| only hide the userform and let parent userform take care of unloading it End Sub

  1. frmEmployee

    其中,类似于 frmManager

    • 您将使用其 UserForm_Activate 事件处理程序向员工介绍此表单

Private Sub UserForm_Activate() MsgBox "Please fill out your self evaluation under the column: Self Evaluation Score" End Sub

  • 如果你有一个 "Close" 按钮(假设你称它为 BtnClose),那么它的 Click 事件处理程序将是:

Private Sub BtnClose_Click() Me.Hide '<--| only hide the userform and let parent userform take care of unloading it

最后是 "main" Sub editEvaluation()

的代码
Option Explicit

Sub editEvaluation()

    'Show "main" type userform
    With frmType
        .Show '<--| this userform has to collect user initial data and then call "child"" userforms accordingly
    End With
    Unload frmType '<--| have this sub unload its "child" userform

End Sub