Hmm...well...alright...
Ok, firstly we have the declaration of the function FormByName. It expects up to two parameters to be passed, the second of which is optional (i.e you don't
have to pass it,and if you don't it takes a default value), and returns a Form object. The first parameter is a string, and is expected to contain the name of a form that exists in the project (if the string contains the name of a non-existant form, then the function returns Nothing). The second parameter is a boolean flag that indicates whether we want a new instance of the Form returned or an existing instance
Code:
Public Function FormByName(strFormName As String, Optional NewFormRef As Boolean = False) As Form
Next we declare a couple of working variables, one for use in an enumeration loop, the other as a temporary result holder
Code:
Dim frm As Form
Dim frmLoaded As Form
Ok, now we're into the routine proper. If the caller has either left out the second parameter or set it to false we're going to enumerate through the list of already loaded forms. VB forms are generally in one of two states, loaded or unloaded; the project's Forms collection only contains those forms that are loaded. As we go through the collection we check whether the name of the form from the collection matches the name we are looking for. If there is a match we set our temporary result holder to that form. Now, theoretically, upon finding our first match we could exit the enumeration with an Exit For, but there may be more than one instance of the same form loaded (basically a form is just a class with a visual component, so you can have as many instances of the same form as you like) and, for a variety of reasons, this function was written to pick up the most recent instance so we keep going until we've enumerated all of the loaded forms
Code:
If Not NewFormRef Then
[COLOR=green]' Make sure we only load form once by returning reference
' to form if it is already loaded.[/color]
For Each frm In Forms
If LCase(frm.Name) = LCase(strFormName) Then
Set frmLoaded = frm
End If
Next
End If
At this point frmLoaded either contains Nothing, or the most recent instance of a loaded form that has the name you are looking for. If it is Nothing, then either we opted not to try and get a reference to an already loaded form (NewFormRef was False), or the form is not already loaded. So now we need to search amongst all the project forms. Trouble is that there is no collection of these available for us to enumerate. So how do we do it? Well, essentially we cheat. We tell VB to load a form of the name we are looking for. Either that form successfully loads (in which case our temporary result holder holds a reference to the form) or it fails with an error - but we've trapped the error and instructed VB to skip to the next line if one occurs (whereupon we immediately clear down the error handler). In this situation our temporary result holder holds Nothing
Code:
If frmLoaded Is Nothing Then
On Error Resume Next [COLOR=green]' skip problem if no form found. Calling procedure should error handle[/color]
Set frmLoaded = Forms.Add(strFormName)
On Error GoTo 0
End If
And finally. By this point our temporary result holder (frmLoaded) is either Nothing or holds a reference to a loaded form (either to a form that was already loaded or to a form that we have just loaded - depending on circumstances). We return this value from the function
Code:
Set FormByName = frmLoaded
End Function