Solved

pass one string value as a form's name to call a form's method??

Posted on 2003-10-30
29
321 Views
Last Modified: 2010-05-03
Hi! All experts,

I stored forms' names (frmFormName1, frmFormName2,...etc.) in a string field in a table.
I'd like to pass one string value as a form's name to call a form's method (ex: Load frmFormName), or change a form's property??
Is there any way to do that? Please advise!! Thanks in advance!!

Below are my codes:
==============================
Dim vStr As Variant
Dim vFrm As Form
Dim FormRS As New ADODB.Recordset
Dim FormSQL, vSys As String
    'codes to get a string value from table
    .......
    .....
   
   vStr = trim(FormRS!FormName)   'i.e."frmABC"
   
  'error
   Set vFrm = vStr  
   
  'succeed if form name hard coded,  but not I want.
   Set vFrm = frmABC
   
   vFrm.Show
0
Comment
Question by:mingichu
  • 10
  • 9
  • 6
  • +1
29 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9652328
See Callbyname function at MSDN help (Sorry i haven't it)
0
 

Author Comment

by:mingichu
ID: 9652614
I found CallByName is used as below:
CallByName(Object as Object, ProcName as string, CallType as vbCallType,  arg() as Variant)

However I couldn't pass a string to the 1st part: Object......

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9652630
CallByName will only call a method or property of an object by name.  It still needs a valid object reference variable.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9652659
I have seen code samples where you build a collection of the forms, using the name for the key:

Dim cForms As New Collection

cForms.Add frmABC, "frmABC"
cForms.Add frmDEF, "frmDEF"

Then:

Set vFrm = cForms(vStr)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9652725
i like last one!
It's a pity that we cannot use forms collection itself.
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9653282
You dont need to store the Name string in the collection. You can anyways get it from the .Name property.

Richie, why cant we use the Forms collection?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9653315
The forms collection only contains references to the loaded forms, so you would have to load every form before trying to reference it.  Also, the collection doesn't have a key, so you can't get a reference by name.
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9653348
But you can iterate through the collection and use the .Name property. I hope I am not wrong.

For Each frm in Forms
  if frm.Name = "Blahblah" Then
    ' got the form we are looking for.
  Endif
Next frm

And anyways, the OP wants to be able to change a forms property or call a methed, using the form name. This means that the form already has to be loaded. So, we can safely use the Forms collection.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9653381
Yes, you are correct.  That would work if you preloaded the form.  When I am working with 30+ forms, I like the ease of not preloading forms, and then iterating through the entire collection.  

But my way means that you have to have 30+ lines of code to build the collection.  So, it becomes six of one, and half dozen of another.  

Your choice.
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9653487
As far as I know, if you call a method of a form thats not been preloaded, then VB loads the form for you.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9653517
Okay, but to find a reference to a form by looping through the Forms collection, it has to be loaded first.
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9653532
Yes you are right :)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9657145
i haven't vb here so i cannot test ir, just guessing:

To load a form from its name it is necessary that that form ALREADY belongs to the project, right?
so...
dim frm as form

set frm = forms("formname_goes_here")

frm.windowstate = vbmaximized

is that right?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9657811
I feel like a broken record.

The forms collection can only be accessed by using an integer 'Index' or by iterating through the collection with a Form variable.

Forms(1)

or

For Each frm in Forms
Next frm

A form is part of the Forms collection only after it is loaded.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9658153
well, as i said earlier, i cannot test it:
function GetForm(sFormName as string) as form'???? or as object
dim i as integer
set GetForm = nothing
for i=0 to forms.count '????
     if forms(i).name = sFormName then
        set GetForm = forms(i)
        exit for
     end if
next i

end function
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9658729
Richie, a form doesnt become a part of the Forms collection unless its loaded. So, you'll never get a form thats not loaded.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9658751
I love it when the asker starts a question, and then disappears into the background, never to be heard from again.  And the discussion continues on without them.  

What a wonderful concept!
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9658784
I think I better go back to newsgroups.
0
 

Author Comment

by:mingichu
ID: 9672876
Sorry!! Guys,

I left early from work last Friday (10/31) and didn't get check your great input until now.......
I'm still working on it by applying your suggestion......
0
 

Author Comment

by:mingichu
ID: 9673037
Hi!

I got "Object Required" error by trying both codes below.....

1.
Set vFrm = cForm(i)
2.
Set vFrm = cForm(vStr)

I guess there is no way to load or call a form using an index or a string before the form is loaded.....

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9673039
How did you add the objects to cForm?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9673054
I know that it works, because I have used it on occasion.  Maybe if you post your code, I can tell what you are doing wrong.
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9673419
Have you populated the cforms collection already?
0
 

Author Comment

by:mingichu
ID: 9673672
Here is  my codes. I tried to load the form.
Thanks!!!!

=============================

Public sub Test()

Dim cForm As New Collection
Dim vFrm As Form
Dim vStr As Variant
Dim vCtr as Integer
Dim FormSQL As String
Dim FormRS As New ADODB.Recordset

   'codes to get a string value from table
    vCtr = 1
    FormSQL = "SELECT DISTINCT FormName FROM FormControlTable "
    FormRS.Open FormSQL, PConn, adOpenDynamic    .....
    Do While Not FormRS.EOF
          vLoadStr = trim(FormRS!FormName)      'i.e."frmABC"  
          cForm.Add vLoadStr, vLoadStr
                If vLoadStr = mnufrm(0).Caption Then  'mnufrm is a menu
       
          Set vFrm = cForm(vCtr) '*** error...Object Required
          Set vFrm = Forms(vStr) '*** error...Object Required
          
          CallByName vFrm, "SHOW", VbMethod
                    '*** error...Object Required
          
          End If
                vCtr = vCtr + 1
          FormRS.MoveNext
    Loop
    FormRS.Close
   
End Sub    
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9673867
How do you initialize mnufrm? How do you initialize vStr?

Try Set vFrm = cForm(vCtr - 1), maybe the index of the collection starts from 0.

Also, why do you get the form names from the DB? Cant you store it in a collection directly?
0
 

Author Comment

by:mingichu
ID: 9674412
Hi!

The program populates mnufrm(0),....mnufrm(n) according to the values FormName field in FormControlTable.
And, the correction:  vStr = Trim(FormRS!ReqForm).  
(please see codes below. Forget vLoadstr. Sorry!)
I just tried it again. The index of the collection starts from 1.

The reason we use the value ("frmABC", "frmDEF", "frmGHI".....) from the table is that we don't want to change the codes each time we add a form to this project. We expect the new form will be similar to the existing ones in this system......

Thanks!!!
===================
Public Sub Test()
Dim cForm As New Collection
Dim vFrm As Form
Dim vStr As Variant
Dim vCtr As Integer
Dim FormSQL As String
Dim FormRS As New ADODB.Recordset

   'codes to get a string value from table
    vCtr = 1
    FormSQL = "SELECT DISTINCT FormName FROM FormControlTable "
    FormRS.Open FormSQL, PConn, adOpenDynamic
    Do While Not FormRS.EOF
        vStr = Trim(FormRS!ReqForm)      'i.e."frmABC"
        cForm.Add vStr, vStr
        If vLoadStr = mnufrm(0).Caption Then  '  mnufrm(0) is a menu        
            Set vFrm = cForm(vCtr) '*** error...Object Required
            Set vFrm = Forms(vStr) '*** error...Object Required
           
            CallByName vFrm, "SHOW", VbMethod
            '*** error...Object Required
       
        End If
        vCtr = vCtr + 1
        FormRS.MoveNext
    Loop
    FormRS.Close

End Sub
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9674481
Try accessing cForm with the key instead of the index
Set vFrm = cForm(vStr)

Also, try tracing this program, comment the Set statements in the loop and CallByName call in the loop. Tell me if the collection gets populated correctly
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 9678262
cForm.Add frmABC, frmABC.Name
cForm.Add frmDEF, frmDEF.Name
cForm.Add frmGHI, frmGHI.Name
0
 

Author Comment

by:mingichu
ID: 9678478
It populates fine (cForm.Add vStr, vStr)
I can see the value of each cForm(vStr) or cForm(1)...cForm(n).
But each item is still not considered as a "form"... Because I use a string (vStr) as an item of this collection instead of a form (frmABC, mentioned by TheLearnedOne) .
I guess we can not get a form or object dynamically from the value of the table. We have to specified it in the codes....

But thanks a bunch for your help!!

=================
Public Sub Test()
Dim cForm As New Collection
Dim vFrm As Form
Dim vStr As Variant
Dim vCtr As Integer
Dim FormSQL As String
Dim FormRS As New ADODB.Recordset

   'codes to get a string value from table
    vCtr = 1
    FormSQL = "SELECT DISTINCT FormName FROM FormControlTable "
    FormRS.Open FormSQL, PConn, adOpenDynamic
    Do While Not FormRS.EOF
        vStr = Trim(FormRS!ReqForm)      'i.e."frmABC"
        cForm.Add vStr, vStr

        vCtr = vCtr + 1
        FormRS.MoveNext
    Loop
    FormRS.Close

End Sub
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now