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.
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.
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:
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.
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.