Solved

Passing Form name to module

Posted on 2004-10-10
8
136 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
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
 

Author Comment

by:eciabattari
ID: 12272406
Thanks... worked perfect.
0
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!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now