在用户表单之间传递变量的错误--VBA

问题描述 投票:0回答:1

我有一个用户表单(frmMatrix),用户可以在其中输入当前的热指数和感兴趣的人群,并返回建议的保护措施的声明。接下来,他们被询问是否愿意在我的数据输入表格(frmForm)中记录这些保护措施。单击“是”后,我希望我的数据输入表单显示“热度指数”和“感兴趣的人群”字段,其中填充了用户在 frmMatrix 表单中输入的值。

我能够成功打开数据输入表单,但加载到“热度指数”和“感兴趣人群”字段中的值是用户*两个*条目之前输入的值。

例如,假设用户在 frmMatrix 中连续输入以下内容:

#1:“83”和“运动员”

#2:“85”和“孩子”

#3:“87”和“户外工作者”

当用户在输入 #2 后回答“是”打开数据输入表单时,数据输入表单将在热度指数和感兴趣人群字段中显示值“83”和“运动员”(在示例 #1).

输入条目 #3 并选择“是”后,数据条目表单将打开,其中包含条目 #2 中的值(“85”和“儿童”)。

为什么会发生这种情况?将热度指数和感兴趣人口的更新变量传递到数据输入表单时似乎存在问题。我复制了下面的代码。

Option Explicit
Public index As Integer
Public category As String
Public population As String

Private Sub cmdGo_Click()

    If ValidateMatrixEntries() = True Then
    
        index = frmMatrix.txtIndex.Object.Value
        population = frmMatrix.cmbPopulation.Object.Value
        '--------------------------------------------------------------------------------------------
    
        'Heat Stress Category 1
        If index < 80 Then
        
            category = "Category 1"

            '(nonrelevant code follows for outputting protective measures)
            '--------------------------------------------------------------------------------------------

        Dim AnswerYes As String
        Dim AnswerNo As String
        
        AnswerYes = MsgBox("Would you like to record these actions in the log?", vbQuestion + vbYesNo, "Record Entry")
        
        If AnswerYes = vbYes Then
            Call Preset_Form
            Unload frmMatrix
        Else
            Unload frmMatrix
        End If
    End If
End Sub

Public Sub Preset_Form()
    frmForm.Show
    frmForm.txtIndex.Value = index
    frmForm.cmbPopulation.Value = population
    frmForm.cmbAction.Value = category
End Sub

图片(如果有帮助): frmMatrix

Data Entry form (frmForm)

excel vba userform
1个回答
0
投票

您需要更改

Preset_Form
逻辑以在作业后显示表单:

Public Sub Preset_Form()

    frmForm.txtIndex.Value = index
        
    frmForm.cmbPopulation.Value = population
        
    frmForm.cmbAction.Value = category
        
    frmForm.Show

End Sub

原因是

Show
直到表单关闭后才返回,因此不会分配值。

© www.soinside.com 2019 - 2024. All rights reserved.