Reach a form created at runtime within a Function

eros

Board Regular
Joined
May 6, 2011
Messages
90
Dear all,

I have the following code:
Code:
Function ShowMyForm(x As Integer, str As String, dbl As Double)
   Dim Form As New myUserForm
   Load Form
   Form.Label1.Caption = "Test"
   Form.Label2.Caption = "Test"
   Form.Label3.Caption = "Test"
   Form.Show VbModeless
End Function

The function builds a form and loads it. It stays in memory and allows other code execution. There are several forms created in this way during my program execution.

I want to reach out these forms and change some of the controls such as Label2 and Label3.

How can I do this from within another function - not from the function which created the actual form.

Is there a way to sequentially loop the controls in the form -in our case, 3 different label controls?

Many thanks
 
While waiting for a return to my previous message, may I just pose a question here. I am trying to see if a Label control exists on my form before assigning some values. The following code generates #VALUE


Code:
function myFunction()
  Dim ctrlLabel As MSForms.Control
  For Each myForm In UserForms
  Set ctrlLabel = myForm.myLabel1
  ' If gForm.LabelFormTip Is Nothing Then 'this didn't work out, I can understand this 
  If ctrlLabel Is Nothing Then 'this didn't work out either, I can't figure it out why
    MsgBox "myLabel1 does not exists"
  End If
end function
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You need an On Error Resume Next before you attempt to access the control, since a run time error will occur if it doesn't exist.
 
Upvote 0
Can I not check existence via "If ctrl Is Nothing"? This works fine with forms and controls and I thought Label is also a control.
 
Upvote 0
You can but only with an error handler since you are attempting to access a property of the form that does not exist.
 
Upvote 0
As long as "if ctrl Is Nothing" check does not work, I cannot figure it out how an error-handler would help me.

How can I combine an error-handler with "if ctrl Is Nothing" check?
 
Upvote 0
Exactly as I said - add On Error Resume Next before you try to access it:
Code:
Function myFunction()
  Dim ctrlLabel As MSForms.Control
  For Each myform In UserForms
  On Error Resume Next
  Set ctrlLabel = myform.myLabel1
  ' If gForm.LabelFormTip Is Nothing Then 'this didn't work out, I can understand this
  If ctrlLabel Is Nothing Then 'this didn't work out either, I can't figure it out why
    MsgBox "myLabel1 does not exists"
  End If
  Next myform
End Function

As an aside I don't see the point of using a function here since you aren't returning anything from it.
 
Upvote 0
Thank you Rorya,

On Error check works like a charm! I now see your point. Both On Error check and Is Nothing check are what I need.

As for using a Function instead of a Sub, the code I attached is an extract from a larger function which returns a value. Thank you for the warning though.

Many thanks again.
 
Upvote 0
Just to close the thread out, may I kindly remind my previous message here. You guys being awesome so far! Many many thanks indeed.

Norie,

I hardly think I have options other than UserForms. I have to run a code, checking/verifying several variables on-the-fly. Therefore, I have to use Functions as opposed to Subs. During code exection I need to see the instant values which the global variables and arrays hold. And now, I am trying to hold all these variables in a listbox-looking table which is updated as the vba code runs within functions. Well, I have several public functions which contribute evaluation of variables and access the form to update relevant variables.

A-listbox-looking table means 10 rows (i.e. items) each having 3 columns of data. I just need this dynamic table to be shown on the form as my output window of the vba code. Number of rows may reach up to 10 during execution, but it may as well be less than 10. Therefore, when specific conditions are met, I need to add or remove some rows from the table. Using a listbox appeared to be a good hint as it would allow me add/remove items easily; however, in my trials, I could not reach listbox items across functions. It simply generates #VALUE. However, it works well with sub, but I rely on the code within my functions to evaluate listbox variables. Therefore, I think I will have to give up the listbox option.

Other than the listbbox option, I have another one -as you call it, a "messy option". Do you really think there are other ways to achieve the same? So far, Tweedle's code seems to be the most sensible way forward. I have already adapted partial code into my functions and it worked well.
Unless there is a better suggestion, I will proceed with the messy option. It feels like I am reinventing the wheel. Therefore, before I go further I just wanted to consult you professionals for the way to follow.

Kind regards,
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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