[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

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

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
mingichu
Asked:
mingichu
  • 10
  • 9
  • 6
  • +1
1 Solution
 
Richie_SimonettiIT OperationsCommented:
See Callbyname function at MSDN help (Sorry i haven't it)
0
 
mingichuAuthor Commented:
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
 
Bob LearnedCommented:
CallByName will only call a method or property of an object by name.  It still needs a valid object reference variable.
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.

 
Bob LearnedCommented:
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
 
Richie_SimonettiIT OperationsCommented:
i like last one!
It's a pity that we cannot use forms collection itself.
0
 
ashooooCommented:
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
 
Bob LearnedCommented:
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
 
ashooooCommented:
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
 
Bob LearnedCommented:
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
 
ashooooCommented:
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
 
Bob LearnedCommented:
Okay, but to find a reference to a form by looping through the Forms collection, it has to be loaded first.
0
 
ashooooCommented:
Yes you are right :)
0
 
Richie_SimonettiIT OperationsCommented:
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
 
Bob LearnedCommented:
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
 
Richie_SimonettiIT OperationsCommented:
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
 
ashooooCommented:
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
 
Bob LearnedCommented:
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
 
ashooooCommented:
I think I better go back to newsgroups.
0
 
mingichuAuthor Commented:
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
 
mingichuAuthor Commented:
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
 
Bob LearnedCommented:
How did you add the objects to cForm?
0
 
Bob LearnedCommented:
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
 
ashooooCommented:
Have you populated the cforms collection already?
0
 
mingichuAuthor Commented:
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
 
ashooooCommented:
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
 
mingichuAuthor Commented:
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
 
ashooooCommented:
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
 
Bob LearnedCommented:
cForm.Add frmABC, frmABC.Name
cForm.Add frmDEF, frmDEF.Name
cForm.Add frmGHI, frmGHI.Name
0
 
mingichuAuthor Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 10
  • 9
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now