?
Solved

Passing Form name to module

Posted on 2004-10-10
8
Medium Priority
?
144 Views
Last Modified: 2010-05-02
Hi - is there  a way in which to pass a form name to a module where the command executed can be multiple form names?

I know that you can pass general variables, like Caption; however, I am unable to hide specific label.

I didn't want to have to duplicate the code for each form.

Thanks for the help.

Information:
I have two forms: frmCommercial and frmTechnical.  Both forms have a label called "lbl1a".  

'frmCommercial
Private Sub Command1_Click()
     Call fGetFormName(frmCommercial)
End Sub

'frmTechnical
Private Sub Command1_Click()
     Call fGetFormName(frmCommercial)
End Sub

'------ module -----------------------------
'one module that used for multiple forms
Function fGetFormName(iform As Form)    
    iform.lbl1a.visable = True  <---------- THIS IS WHERE I GET THE ERROR, Run-time error 438
End Function
0
Comment
Question by:eciabattari
[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 10

Accepted Solution

by:
fds_fatboy earned 1000 total points
ID: 12272380
In both forms change the code to

Private Sub Command1_Click()
     Call fGetFormName(Me)
End Sub

and

    iform.lbl1a.visable = True  
should read
    iform.lbl1a.visible = True  
0
 
LVL 5

Expert Comment

by:waelothman
ID: 12272383
you can pass Me.Name as string to the function
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12272394
How about passing the form name instead of the form itself?  You can then use an If or Case to set the correct label's visible property.  Something like:

Function fGetFormName(strFormName as String)
    if strFormName = frmCommercial Then
        frmCommercial.Lbl1a.Visible = True
    Else
        frmTechnical.Lbl1a.Visible = True
    End If
End Function

As a side note, I can't help but notice that in your code you've misspelled "visible" as "visable" and that you're using a function to accomplish this but don't appear to be returning anything.  Are you sure that's not the source of your error?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:eciabattari
ID: 12272406
Thanks... worked perfect.
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12272428
PS: the reason you get this error at run time and not at compile time is due to late binding the form/control.

If you wanted to make use of intellisense/compiler early bound error checking you could have done something like this:

Function fGetFormName(iform As Form)    
    Dim lbl1a As VB.Label

    Set lbl1a = iform.lbl1a
    iform.lbl1a.Visible = True
    Set lbl1a = iform.lbl1a
End Function

Another clue would have been when the initial v wasn't capitalised in the word "visable".

Also, Why do you do this in a Function, wouldn't a Sub be better. A sub returns void (in C parlance) but this Function retuns an implicitly declared variant.

While I'm at it,  

    iform.lbl1a.visible = True

actually shows the label - if you want to hide it try
    iform.lbl1a.visible = False
 
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12272453
BDF:

>> How about passing the form name instead of the form itself?  You can then use an If or Case to set the correct label's visible property.  Something like:<<

Why do that? VB string handling is poor and passing a form name would require creating a creating a new memory buffer, copying the string into it and passing the pointer to the string as a Long Integer.

Passing the Form reference is much neater. You just pass the Object pointer to the form Long Int. Also if you pass the Form reference, it is easy to get the name of the form. Passing the form name will not work in MDI environments where you might get two instances of the same form with the same name.
0
 
LVL 76

Expert Comment

by:David Lee
ID: 12272521
"Why do that?"
Because some folks find it easier conceptually to deal with the name of something than with an object reference.  I was illustrating the idea of passing something other than the object itself.  The passed item didn't have to be a string,it could've been a number (0 for one form and 1 for the other), etc.  

"Passing the form name will not work in MDI environments where you might get two instances of the same form with the same name."
You're absolutely correct.  
0
 
LVL 10

Expert Comment

by:fds_fatboy
ID: 12272985
Fair enough.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

650 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