Link to home
Start Free TrialLog in
Avatar of eciabattari
eciabattari

asked on

Passing Form name to module

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
ASKER CERTIFIED SOLUTION
Avatar of fds_fatboy
fds_fatboy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of waelothman
waelothman

you can pass Me.Name as string to the function
Avatar of David Lee
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?
Avatar of eciabattari

ASKER

Thanks... worked perfect.
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
 
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.
"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.  
Fair enough.