I wish to call a form with the form name contained in a variable

using a declaration such as

public myform as form

will work if the formname is used.

but myform will not accept a variable containing the form name

have you tried using the NEW keyword.

I have

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

Try using this.

public myform as Form
 myform = New Form1
call thisisasub(myform)

not sure what that will achieve

it seems to me that you still require explicit use of the formname

The only reference I have to the formname is
mainform.list1.list(list1.listindex)

as the form is selected from a listbox

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...
Me.List1.AddItem "Form2"
ReDim myForms(0)
Set myForms(0) = New Form2

Me.List1.AddItem "Form3"
ReDim Preserve myForms(1)
Set myForms(1) = New Form3

End Sub

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
        Call thisisasub(myForms(i))
        Exit Sub
    End If
Next i
End Sub

Private Sub thisisasub(parmForm As Form)
Debug.Print parmForm.Name
parmForm.Show
End Sub

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.

Thanks

all the code seems recognisable in VB4 so,

I will try this, if successful, I will post resolved

(and probably a great deal of thankyous)

however, I have never explicitly declared the integer of a for next loop..is the dim i as integer really necessary

most of the code works fine

not sure if there is a difference in VB6, but

if I make any reference like if mymodule.myforms(I).name=me.list1.text then

it returns a form already loaded message

in fact any reference to mymodule.myforms(I).name returns the same

If I turn off sort property of list and keep myforms in same sequence then

call thisisasub(mymodule.myforms(list1.listindex) works

I would like to be able to check the myforms array against the list so any thoughts would be useful

This problem is resolved as far as I need it to be.

some slight adaptions of your code were needed, but you certainly pointed me in the right direction.

Thanks a lot

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.