Solved

Passing Form name to module

Posted on 2004-10-10
8
140 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 250 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…

738 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