Bug with passing variables between user forms--VBA

gc2001

New Member
Joined
Apr 2, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a userform (frmMatrix) where the user enters the current Heat Index and Population of Interest and is returned with a statement of recommended protective measures. Next, they are asked if they would like to make a record of these protective measures in my Data Entry form (frmForm). Upon clicking yes, I would like my Data Entry form to display with the "Heat Index" and "Population of Interest" fields filled with the values entered by the user in the form frmMatrix.

I am able to successfully open the Data Entry form, but the values loaded into the "Heat Index" and "Population of Interest" fields are those entered by the user *two* entries ago.

For example, suppose the user makes the following consecutive entries into frmMatrix:

#1: "83" and "Athletes"

#2: "85" and "Children"

#3: "87" and "Outdoor Workers"

When the user answers "Yes" to open the Data Entry form after making entry #2, the Data Entry form will display with the values "83" and "Athletes" in the Heat Index and Population of Interest fields (the responses given in example #1).

After making entry #3 and selecting "Yes", the Data Entry form will open with the values from entry #2 ("85" and "Children").

Why might this be happening? It seems there's an issue passing the updated variables for Heat Index and Population of Interest into the Data Entry form. I've copied my code below.

VBA Code:
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



pictures of forms if useful:

frm Matrix:
1712772992550.png


frmForm (Data Entry form):
1712773047086.png
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try showing frmForm after you've assigned the values to your controls...

VBA Code:
Public Sub Preset_Form()

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

End Sub

Hope this helps!
 
Upvote 0
By the way, as an aside, there's no need to refer to the Object property of your control. The following would suffice...

VBA Code:
index = frmMatrix.txtIndex.Value

population = frmMatrix.cmbPopulation.Value
 
Upvote 0
Try showing frmForm after you've assigned the values to your controls...

VBA Code:
Public Sub Preset_Form()

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

End Sub

Hope this helps!
That worked! Thanks very much. I had a feeling it would be something small like that.
 
Upvote 0

Forum statistics

Threads
1,215,212
Messages
6,123,656
Members
449,114
Latest member
aides

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top