?
Solved

Passing  a form name dynamically through variable

Posted on 2003-03-25
8
Medium Priority
?
139 Views
Last Modified: 2010-05-01
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
Comment
Question by:tsravank
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8202549
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 8203582
Dim f   As Form

Set f = Forms.Add(strFormName)
f.Show
0
 

Expert Comment

by:AlbertZee
ID: 8208475
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
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.

 

Author Comment

by:tsravank
ID: 8209140
Thanks Arthur,

I am getting an error. Can you please give me complete code pls
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8209683
what error are you getting, and what code are you using?  I gave you several possibilities.

Arthur Wood
0
 

Author Comment

by:tsravank
ID: 8210023
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
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 80 total points
ID: 8210326
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
 

Author Comment

by:tsravank
ID: 8223589
Thanks a lot
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question