Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set frm = Forms(FormName)??? 2

Status
Not open for further replies.

jgoodman00

Programmer
Jan 23, 2001
1,510
I have written a function which loops through the controls in a form. However, I dont want to hard-code the form name. Instead I want to pass this into the function as a parameter.

In Microsoft Access I could do this as follows:


Sub subName(strFormName as String)
Dim frm as Form
Set frm = Forms(strFormName)

However, this does not appear to work in VB6.

How do I achieve this? James Goodman
 
You need a function similar to the following. Then you can do

Set frm = FormByName(strFormName)


[tt]
Public Function FormByName(strForm) As Form
Dim myForm As Form

' Check to see if form is already loaded
For Each myForm In Forms
If myForm.Name = strForm Then Exit For
Next

' Load named form if not already loaded
On Error GoTo BadForm
If myForm Is Nothing Then Set myForm = Forms.Add(strForm)
On Error GoTo 0

Set FormByName = myForm
Exit Function

BadForm:
If Err.Number = 424 Then Call Err.Raise(vbObjectError + 424, "FormByName", strForm + ": no such form exists in the project", "", 0)
End Function
 
One form class (name) can be instantiated more than once so the "name" can not be used as a key. If you have instantiated (loaded) just one by using the implicit object variable with the same name as the form then
Dim frm as Form
For Each Form in Forms
if Lcase$(frm.name) = Lcase$(strFormName) then exit for
Next
'Frm Is Nothing if not found

How to get two instances of the same form class?
Dim frm1 as Form1
form1.show ' 1st
Set frm1 = New Form1
frm1.show ' 2nd Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top