Link to home
Start Free TrialLog in
Avatar of tsravank
tsravank

asked on

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.
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

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
Dim f   As Form

Set f = Forms.Add(strFormName)
f.Show
Avatar of AlbertZee
AlbertZee

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....
Avatar of tsravank

ASKER

Thanks Arthur,

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

Arthur Wood
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..."
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot