Fail to Access Multiple Userforms in Runtime

eros

Board Regular
Joined
May 6, 2011
Messages
90
Hi everyone,

I have no problem in using a single userform which consists of several label controls which I use to reveal several data produced on the fly.

However, when I try to define a second userform and reach both userforms to update relevant label controls in each userform, then I have a problem: I cannot reach the label controls in the second userform in order to update them during program execution.

In the decleration, I define my M and R userforms as below, each userform has different layout, content and number of label controls to be updated on the fly.

Code:
Public mForm As UserFormM 'I design M and R userforms on the design section
Public rForm As UserFormR ' So they are already there

In the main function I first initialize both userforms like that: Initialization is done only once and at the very beginning of my main VBA function.

Code:
Function InitializeUserFormM()
  Set mForm = New UserFormM
  Load mForm
  mForm.LabelFormTip.Caption = "M"
'
' here many initial value assignments to label controls in the userform
'
  mForm.Show vbModeless
end Function

The above is identical for the R Userform so I won't repeat here again. I assign "M" to the label control called LabelFormTip in the userform M to check during the update process if the form is the one I need. One form holds "M" the other "R" so that I know what userform I am updating.

And here is how I update the userforms:

Code:
Sub UpdateMForm()
  Dim ctrlLabel As MSForms.Control
  For Each mForm In UserForms
    On Error Resume Next
    Set ctrlLabel = mForm.LabelFormTip 
    If Not ctrlLabel Is Nothing Then
      If mForm.LabelFormTip.Caption = "M" Then  
        '
        ' here many value assignments to label controls in the userform
        '
      End If
    Else
      MsgBox "No M LabelFormTip" 
    End If
  Next
End Sub

A similar Sub does exist for the UserFormR like above. So I don't repeat it here.

I was hoping for each different userform, the above code would check each userform in the memory to see if it is an M or R type and based on the value held in the LabelFormTip control on each userform it would update relevant label controls. There is only one M and R userforms even though For Each mForm In UserForms code suggests there are many for each type. I just want to keep it as a reserve for further development in the future.

When I had only one userform, namely the UserformM, everything was ok, and it was working great. When I added the second userform UserformR, I see the form loaded, but the contents never change during program execution.

I also noticed and much to my surprise, if I first call updateRForm sub before updateMForm, this time both userforms are not updated at all. I didn't think sequence of a sub call would create such a big difference.

Another slight problem I am facing is that when I minimize Excel and all open workbooks etc, my already perfectly working M Userform does not come back and appear again. I simply lose it. The R comes back but as I say no update occurs on it.

Can anyone has an idea as to what the problem is and how I can get around it.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,956
Messages
6,175,613
Members
452,661
Latest member
Nonhle

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