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

Loading FORM dynamically 3

Status
Not open for further replies.
Aug 16, 2001
26
0
0
NZ
I have a string variable which contains a FORM Name. Now I wanted to load the FORM from the value in the string variable. My code is something like this...

****************************************************
Dim gForm as Form
Dim mFormName as String

mFormName = "MyForm"
Set gForm = mFormName
gForm.Show
****************************************************

I do not want to use any CASE SELECT statments, becos there will be atleast 50 Forms and more when my Prj is completed. Can somebody please guide how to handle this situation.

 
The following should do the trick:
[tt]
Option Explicit

Public Function FormByName(strFormName As String) As Form
Dim frm As Form
Dim frmLoaded As Form

' Make sure we only load form once by returning reference
' to form if it is already loaded
For Each frm In Forms
If LCase(frm.Name) = LCase(strFormName) Then
Set frmLoaded = frm
End If
Next
If frmLoaded Is Nothing Then
On Error Resume Next ' skip problem if no form found. Calling procedure should error handle
Set frmLoaded = Forms.Add(strFormName)
On Error GoTo 0
End If
Set FormByName = frmLoaded

' Still nothing? Then no form by this name exists in the project
If FormByName Is Nothing Then
Err.Raise vbObjectError + 512 + 1, "FormByName", "No such form is available in the project"
End If
End Function
[/tt]

Your code now becomes:

****************************************************
Dim gForm as Form
Dim mFormName as String

mFormName = "MyForm"
Set gForm = FormByName(mFormName)
gForm.Show
****************************************************
 
Well, you beat me to it.

Just two things:

1. I would put an Exit For after the line:
Set frmLoaded = frm

2. You may want a form to be loaded more than once, so maybe pass an optional boolean to determine if this is allowed, and if so, then skip the first part:

Public Function FormByName(strFormName As String, Optional bMultiLoadAllowed As Boolean = False) As Form

If Not bMultiLoadAllowed Then
For Each frm In Forms
If LCase(frm.Name) = LCase(strFormName) Then
Set frmLoaded = frm
Exit For
End If
Next frm
End If


[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Probably only beat you because I merely cut and pasted one of my earlier posting on this subject...

Both the suggested modifications look like reasonable improvements to me.
 
this one really help me too Please pardon the grammar.
Not good in english.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top