say I set up a sub as follows sub thisisasub(source as form) and my form is called myform which I have put into a variable called myvariable
if I call it thus call thisisasub(myvariable) it does not work
if I call it like this call thisisasub(myform) it does
I need to reference my form using the variable
Unfortunately, VB is very good at hiding the API calls needed to create a class dynamically. In VB when you create a form it becomes a referenceable class, so you have to instantiate it explicitly.
As an alternative, you might consider using an array to hold instances of your forms, then test the value from the listbox in its click event, and show the appropriate form? Here's what I mean:
Dim myForms() As Form ' or, you can put this in a module as a global
Private Sub Form_Load()
' just setting up...
Set myForms(0) = New Form2
ReDim Preserve myForms(1)
Set myForms(1) = New Form3
Private Sub List1_Click()
' just a sample...could be done better...
Dim i As Integer
For i = 0 To UBound(myForms)
If myForms(i).Name = Me.List1.Text Then
Private Sub thisisasub(parmForm As Form)
This assumes that you have already built Form2 and Form3 somewhere. Keep in mind too that in the array of forms, each form name is the CLASS NAME of the form, not the instance name...you'll have to do something else clever if you want to have multiple instances of each type of form (caption? tag?).
Anyway, hope this helps some.
EDIT: I had to do this in VB6...I don't think array and form handling changed between VB4 and 6, so I think this technique will still work.
Variables as Form Names in VB4
While, I appreciate that the thread is marked as solved, I believe that some information should be made available to stop others from falling into the same pit as I did. I marked the thread as solved because the solution offered pointed me in the right direction.
In the solution offered, the ‘New’ keyword was suggested. Unfortunately the ‘New ’ keyword loads a second copy of the form and the consequence is that any reference to it loads it twice. This can be proved by setting up the form with an image and create several more in an array at run time. When the solution is run, it throws up a message “object already loaded”. This is because they are created on the first loading of the form and are still there at the second loading..If you remove the ‘New ‘ keyword only one copy of the form is used and as a result this fault is avoided.
A comparison is made between the listbox list and the myforms collection because the listbox could be in a different order to the myforms collection...so me.list1.text is compared with myforms.name.
But every-time myforms.name is used, the form has to be loaded (though not visible) to allow the ‘Name’ property to be accessed.. If you have a large number of forms, therefore, this comparison process could be significant and the time taken noticeable. The solution is to create a new collection (not of type form) using the same filenames and keep it in the same order as the forms collection.
The comparison is therefore done using the listbox and the new collection, when a hit occurs the index is noted and then used to access the myforms collection.
I would love to hear from anyone with a more elegant solution.
root = tkinter.Tk()
buttons=[i for i in range(10)]
#If specific button is pressed, output "YES"
for num in buttons: