Connecting Tech Pros Worldwide Forums | Help | Site Map

Open a form with a specific name

Newbie
 
Join Date: Jan 2007
Posts: 3
#1: Jan 31 '07
Hi,

I have a pretty basic question that I can't seem to figure out. I'm trying to open a form with a specific name that changes depending on the value of a cell.

For instance (simplified)...

Expand|Select|Wrap|Line Numbers
  1. sub openForm()
  2.  
  3.      dim formNumber as integer
  4.      formNumber = cells(1,1)
  5.  
  6.      dim fileName as string
  7.      fileName = "UserForm_" & formNumber
  8. End Sub
I now want to open the form named 'fileName' as defined above. How can I do this???


Thanks!!!

iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
#2: Jan 31 '07

re: Open a form with a specific name


Quote:

Originally Posted by darren28

Hi,

I have a pretty basic question that I can't seem to figure out. I'm trying to open a form with a specific name that changes depending on the value of a cell.

For instance (simplified)...

sub openForm()

dim formNumber as integer
formNumber = cells(1,1)

dim fileName as string
fileName = "UserForm_" & formNumber

I now want to open the form named 'fileName' as defined above. How can I do this???

end sub

Thanks!!!


Can you just set a Caption of a new form to your fileName?

Like this:

[PHP]Dim frm As New Form1

frm.Caption = fileName
frm.Show[/PHP]
Newbie
 
Join Date: Jan 2007
Posts: 3
#3: Jan 31 '07

re: Open a form with a specific name


hmmm...couldn't get that to work. what is form1? my vb didn't recognize that.

i'm assuming that all the userforms are already created and existing in the vbproject. however, the user determines which form to open based on the value of the cell. therefore, assuming the user inputs "36", the exiting form named "UserForm_36" will open. I'm hoping there's an easy way to do this w/o doing 50+ case/else statements.

thx!
Lives Here
 
Join Date: Oct 2006
Posts: 1,626
#4: Jan 31 '07

re: Open a form with a specific name


Are there that many forms or are you using the same form with a different caption?
Newbie
 
Join Date: Jan 2007
Posts: 3
#5: Jan 31 '07

re: Open a form with a specific name


there will be close to that many forms although not all will be imported/loaded
Lives Here
 
Join Date: Oct 2006
Posts: 1,626
#6: Feb 1 '07

re: Open a form with a specific name


Quote:

Originally Posted by darren28

there will be close to that many forms although not all will be imported/loaded

Then you need to loop through the forms collection to find the one you need
Expand|Select|Wrap|Line Numbers
  1. Dim frm As Form
  2.  
  3. formName = "UserForm_" & formNumber
  4.  
  5. For Each frm In Forms
  6.    If frm.Name = formName Then
  7.       'do what you want to do with this form
  8.       frm.Show
  9.    End If
  10. Next frm
iburyak's Avatar
Expert
 
Join Date: Nov 2006
Posts: 1,017
#7: Feb 1 '07

re: Open a form with a specific name


I didn't think you wher talking about Excel form. I thought is is VB.
But code is the same just default form name is different.

[PHP]
Dim frm As New UserForm1
frm.Caption = fileName
frm.Show
[/PHP]
Reply