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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading forms using a text file 1

Status
Not open for further replies.

epmm001

Programmer
Nov 11, 1998
5
0
0
AU
I am trying to make a form load and show by retrieving the form's name from a text file. I cant use the Forms collection because it only works on loaded forms. What I am trying to do is sort of the reverse of the CallByName function where the object is known, but the property or method is not.

Another way of putting the problem is as follows;

Dim FormToLoad as string

FormToLoad="Form1"

FormToLoad.Show

The above example will obviously not work because of the object mis-match, but I hope I have demonstrated what I am trying to do.

Chris Benton
 
I'm not sure of the code but hopefully I can point you in the right direction. When you have the name of the form what you could do is loop through the forms collection form the current project. the code might look a bit like this

for each FormObj in FormsCollection
if formobj.name = text then
load formobj
exit for
end if
next

Sorry I can't be more specific but hopefully I've pointed you in the right direction. You can get plenty of help on form objects and collections in the MSDN library. Mark

The key to immortality is to make a big impression in this life!!
 
Unfortunately, the Forms collection only contains forms that have already been Loaded. Therefore, as epmm01 correctly says, he cannot use the method you suggest.

However, it is pretty easy to Load an unloaded form by name, as the following function demonstrates:
[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
 
Thanks kindly for the response. A simple and elegant solution for a guy about to slash his wrists.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top