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!

Hiding a Form when the name is stored in a variable 1

Status
Not open for further replies.

SepMan76

Technical User
Apr 5, 2001
45
0
0
US
Is there a way to hide a form when you have the form's name stored in a variable? Obviousely, this doesn't work, but maybe this will help you understand:

strFormName = "Main Form"
strFormName.Visible = False

Thanks,
Chris
 
You can use the Forms collection, indexed by the name of the form stored in the variable, as follows:
Code:
Forms(strFormName).Visible = False
 
Yes... that seems to work when I have the INDEX (an integer) of the Form I want to hide, but not the NAME (a string). Is there anyway to use the NAME (vs. INDEX) in a variable to hide a forn or get the INDEX from the NAME held by a variable so that the above technique can be used?

Thanks again
Chris
 
This function returns a form's index if it's open or -1 if it's not, paste it into a module and then you can use it in your code:
[tt]
Function FindFormIndex(FormName As String) As Integer
Dim i As Integer

'Use For to find form index
For i = 0 To (Forms.Count - 1)
If Forms(i).Name = FormName Then
FindFormIndex = i
Exit Function
End If
Next i

'Return -1 if form is not loaded
FindFormIndex = -1
End Function
[/tt]
You could use it like this, though it would error if the form wasn't loaded so you should trap for that:

Forms(FindFormIndex(strFormName)).Visible = False

HTH Joe Miller
joe.miller@flotech.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top