-
November 5th, 2000, 03:04 PM
#1
Form Loading Via Variable?? - HELP!!
Am writing an application that uses a table to store all MENU Items in it, with descriptions, and a field with either a form name in it or a function name.
The menuing program reads the data from the table and populates the menu form with buttons etc...
The idea is simple. Once the button is selected the app will draw either the FORM NAME to load or a FUNCTION to execute.
THE QUESTION......(s)
1) How do I load a form with the information from the table. Have tried numerous things, but seem to have come up with a brain cramp. Tried this but it didn't work.
DIM newform as form
set newform = .fields(Procedurecall) <--- the var
another e.g.
dim newformstr as string
newformstr = "LOADTHIS"
dim aform as form
set aform = ?????(newformstr)
2) How do I execute a function in the same manner.
e.g. Table says function to be called is 'CALLME'
How do I execute this function.
Any help would be appreciated
-
November 5th, 2000, 05:33 PM
#2
Re: Form Loading Via Variable?? - HELP!!
Hi,
loading a form via its name, i can't help you. Sorry.
But i know how to call a function by its name. Please refer to the documentation of CallByName-function.
Hope this helps
Peter
-
November 7th, 2000, 03:10 PM
#3
Re: Form Loading Via Variable?? - HELP!!
Maybe VB.Net (VB7 ) will allow you to specify a variable containing the name of the form you want to load, but current VB (< 7) does not. You need to specify the name of the form directly like thus
private Sub Command1_Click()
Dim F as Form
' Create a copy of this form
set F = new Form1
F.Show
' or to show a second form
Form2.Show
End Sub
John G
-
November 7th, 2000, 03:40 PM
#4
Re: Form Loading Via Variable?? - HELP!!
You can do this, but you have to think differently. A form is Nothing but an instance of a class. Add a property to your form class to store the formName make this a default property of the form. You then need to have a collection to store the forms. Add the FormName as the Key and the form as as item. You can work with the forms through your own collection.
-
November 7th, 2000, 05:41 PM
#5
Re: Form Loading Via Variable?? - HELP!!
VB doesn't offer any way to dynamically create forms by name, but you can do some clever stuff with class modules to replicate this functionality.
Say you have some forms in your program (form1, form2, form3) which you'd like to create dynamically from a string. I'd recommend that you create a class module which is responsible for creating these forms, eg:
'
' A class module called CFormFactory
'
option Explicit
'
public Function CreateForm(byval sFormName as string) as Form
Dim fFrm as Form
'
Select Case UCase$(sFormName)
Case "FORM1"
set fFrm = new Form1
'
Case "FORM2"
set fFrm = new Form2
'
Case "FORM3"
set fFrm = new Form3
'
End Select
'
Load fFrm
set CreateForm = fFrm
'
End Function
You could then use this class from whereever you like :
eg:
Dim oFactory as CFormFactory
'
set oFactory = new CFormFactory
'
oFactory.CreateForm("form1").Show
'
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
-
November 15th, 2000, 06:22 PM
#6
Re: Form Loading Via Variable?? - HELP!!
Use Add method of Forms collection:
Forms.Add("Form1").Show
option Explicit
private Sub Form1_Click()
Dim x as Form, strName as string
strName = "Form1"
set x = Forms.Add(strName)
x.Show
End Sub
-
November 15th, 2000, 06:25 PM
#7
Re: Form Loading Via Variable?? - HELP!!
I forgot to say:
You'll need VB6 SP3 for Forms.Add to work. Without SP3 it won't work when compiled.
-
November 15th, 2000, 06:44 PM
#8
Re: Form Loading Via Variable?? - HELP!!
Here is a sample with mentioned CallByName function:
option Explicit
private Sub Form_Click()
Dim x as Form
' set x = Forms.Add("form1")
set x = CallByName(Forms, "Add", VbMethod, "Form1")
' x.Show
CallByName x, "Show", VbMethod
' x.Left = me.Left + 100
CallByName x, "Left", VbLet, me.Left + 100
End Sub
-
March 14th, 2003, 11:43 AM
#9
This is something worth to remember...
Have nice days, you BigGuru
Cesare I.
Ps: a note:
Code:
option Explicit
Private Sub Form1_Click()
Dim x as Form, strName as string
strName = "Form1"
'Before adding this form, be sure it is not already added
For Each x in Forms
If x.Name =strName Then
x.Show 'same form found, show it and exit sub
Exit Sub
End If
Next
set x = Forms.Add(strName) 'form not found, add it and show
x.Show
End Sub
Last edited by Cimperiali; October 20th, 2003 at 02:54 AM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
March 14th, 2003, 09:24 PM
#10
Busy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|