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

opening a form where form name is held in a variable 4

Status
Not open for further replies.
Jun 30, 2003
196
GB
I want to open a form but the form name is held inside a variable (well an element of an array called Shortcut1(2).

so i have tryed the following

frmShortcut1(2).Show
and
Shortcut1(2).Show

but this does not work any ideas all you experts:

 
Try a case select statement based on the value of the array item
- B
 
yeah it works if i do a case to match the contents of the variable but this is not an efficient way in my program. Is it not possible to open a form using a variable name?
 
>any ideas all you experts

Sure. This example is a slightly extended version of something I've posted to this forum before. It may seem a little overkill for your issue, but it works...
Code:
Public Function FormByName(strFormName As String, Optional NewFormRef As Boolean = False) As Form
    Dim frm As Form
    Dim frmLoaded As Form
    
    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
    
    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
    Set FormByName = frmLoaded
End Function
An example of calling it might be:

FormByName("Form2").Show

or, using your array (and assuming that frmShortcut1(2) contains the name of a form that exists in the project):

FormByName(frmShortcut1(2)).Show
 
thanks for the code it looks impressive and i am sure is great buti have inserted your function into my module and called it by the following:

FormByName(Shortcut1(2)).Show

But i get a mismatch error occur and i cant figure out why, but then i dont really understand how your function works.
 
What does Shortcut1(2) contain? And are you sure that what you think it contains is actually what it comtains?
 
Alright, let me ask a slightly different question. How is Shortcut1 declared? What is the relavant Dim statement?
 
'In general heading
Dim Shortcut1(3)

'then the following function assigns the values

Public Sub AddShortcut(ShortcutNumber As Integer, Name As String, frmName As String)

Select Case ShortcutNumber

Case 1: Shortcut1(0) = ShortcutNumber
Shortcut1(1) = Name
Shortcut1(2) = frmName
lstShortcutItems.AddItem "1" + " " + Shortcut1(1), 0
lstShortcutItems.RemoveItem (1)
lstShortcutItems.ListIndex = 0

I dont know if it helps but i have run the debugger and the string is definately within the value where it should be.

 
Bingo! It doesn't actually contain what you think it contains...

Your Dim statement has declared Shortcut1 as an array of variants - which is why you are getting the error when calling my function, which is expecting a genuine string (not a variant containing a string, which is a different animal)

Now, you could alter the declaration of my function to get around this, but my advice to you would be to be explicit in your own declarations (it'll save a lot of heartache). SO make

Dim Shortcut1(3)

into

Dim ShortCut1(3) as String

 
Ok thats great ill try that and let ya know how i get on cheers
 
one thing i dont want to make all the elements of the array strings so can i decalare just one of the elements element two as a string and the rest as variants or is this not possible.

 
Forget my last post i changed the whole array to a string and it works!!!!!!!!!!!!!!!!!!

thank you so much that function will save me a lot of time.

Could i please ask one last favour, i have tryed to understand how that function works but i just cant get my head arround it. i dont suppose it would be possible if you could give me a overview of what it does and how it works.

it would be so appreciated if you could

thanks again
have a star
 
No, it is not possible with an array to declare each element as a different type; yes, the only way around this with an array is to Dim it as variant. However, if you are thinking of making each element of the array a different type (or at least of mixing types in the array) you might be better off using a User Defined Type (UDT).
 
I really would appreciate it if you could take the time to let me know how that function works it would be a great help to me.

PLEASE PLEASE PLEASE

 
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
 
Ok that is great, what a function!!!!!

thank you so much for all your help it was great, im not going to be able to take credit for this so i will add a note within the function stating where i got it from.

Thnk you so much you have saved me so much work.

Have a star and if i already given you one have another!

thnaks
 
with that function you gave me, whenever a form is opened using that function i can not properly close them.

For instance i have a button on all my forms called end which does the following

frmCurrent.hide
frmNext.show

but this button doesnt actaully close the form when it is opened via your function it just shows the next one ontop without closing?

Can you help
 
You need to be really certain what frmCurrent and frmNext are actually referencing, as the error looks to be in your code, not in my function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top