• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 141
  • Last Modified:

Passing a form name dynamically through variable

I have to load various Forms depending upon User Request in an application. I want to load through Variable.

Test = someform (form name)
show test

This is not working.. Can I have alternate.
0
tsravank
Asked:
tsravank
1 Solution
 
Arthur_WoodCommented:
private Sub ShowForm(frm as Form)
   frm.Show
End Sub

then

Dim MyForm as Form
Dim FormName as String

FormName = "frmYourForm"
set MyForm = Forms(FormName)
ShowForm MyForm

The format of the Form Show is

Form.Show  

not
 
Show Form


YOu need to declare Test as a variable of Type Form, and then you can assign a reference to a loaded form:

Dim Test as Form

Set Test = Forms(FormName)

if that genrates an error (caused if the Form has NOT been loaded previously - the Form exists in the project, but has not yet been loaded into memory, and thus has NOT been added to the Forms collection), then you need to Load the Form, before Showing it:

Dim Test as Form

On Error Resume Next
Set test = Forms(FormName)
if err.Number <> 0 then
   Forms.Add FormName
   Set Test = Forms("FormName")
End If

Test.Show

AW
0
 
AzraSoundCommented:
Dim f   As Form

Set f = Forms.Add(strFormName)
f.Show
0
 
AlbertZeeCommented:
In case its the Form name you want to change

let me quote Arthur Wood

private Sub ShowForm(frm as Form, frmName as String)
frm.Caption = frmName  
frm.Show
End Sub

Use all Arthur's code but add that - that's if you want to set the form's name at runtime....
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tsravankAuthor Commented:
Thanks Arthur,

I am getting an error. Can you please give me complete code pls
0
 
Arthur_WoodCommented:
what error are you getting, and what code are you using?  I gave you several possibilities.

Arthur Wood
0
 
tsravankAuthor Commented:
Heres my Code
Private Sub Form_Load()
Dim MyForm As Form
Dim FormName As String

FormName = "form2"
Set MyForm = Forms(FormName)
ShowForm MyForm
Form.Show
End Sub

Private Sub ShowForm(frm As Form)
  frm.Show
End Sub

Its giving error "Type Mismatch" at "Set Myform..."
0
 
Arthur_WoodCommented:
ok...my mistake, you can only retrieve members of the Forms collection by INDEX (the Number) assigned to the form when it is actually loaded into memory (which is another point to mention---the forms must have been loaded into memory, not simply included as part of your project.  Until a Form has actually been loaded, the RUN-TIME does not know anything about it)

so change the code like this:

Private Sub Form_Load()
Dim MyForm As Form
Dim FormName As String
Dim FoundIt as Boolean

FormName = "form2"
for Each MyForm im Forms
  if MyForm.Name = FormName then
     FoundIt = true
     Exit For
  End if
Next

If Not FoundIt then
   ' the named form has not already been loaded, so load it now
   Set MyForm = Forms.Add(FormName)

End if

ShowForm MyForm  
Me.Show

End Sub

That should work.

Arthur Wood
0
 
tsravankAuthor Commented:
Thanks a lot
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now