Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing form name to a sub routine

Status
Not open for further replies.

ycim

Programmer
Feb 9, 2005
95
CA
Newbie here! I have been programming in VBA for quite some time, and have recently made the jump to VB. I am trying to pass a form name to a subroutine in a module. The subroutine analyzes records to meet various criteria. Because it is done so often, I make it a generic subroutine. Easy to do in VBA, but it does not work the same in VB.

The subroutine is simply to move to the next record of that form's datacontrol. However, each record has to be looked at to meet some very specific industry criteria.

My subroutine says

Code:
private sub AnalyzeRecord(frmName as form)

when calling it, i am using

Code:
AnalyzeRecord(me.name)

but it is giving me a type mismatch. Any suggestions?
 
Hi:

Try using the call:
Code:
    AnalyzeRecord(Me)

dropping the .Name from the call.

Cassandra
 
Actually, prefreably use:

Call AnalyzeRecord(Me)

or

AnalyzeRecord Me

but try to avoid

AnalyzeRecord (Me)

as it isn't actually doing quite what you think it is
 
Good point, strongm!

Some days it doesn't pay for me to do coding.

Cassandra
 
Not sure if it is a good idea to call method that uses some form to analyze just a record. For me, it is a sign of the faulty design. Any thoughts?
 
Of course, if all you are trying to do is pass the NAME of the form, and not the form object itself, you would want to use something like this:

Declare the sub:
Code:
Private Sub AnalyzeRecord(strNameOfForm as String)

Call the sub:
Code:
Call AnalyzeRecord(Me.Name)
 
Ahhhh!!!!!!....byval and byref....i read a FAQ about that somewhere on here. I had forgotten about the different ways to call a subroutine, and the implications of it.

I think I will have to put on my thinking cap a bit more, and figure out a better way to code this.

Thanks alot!

 
Can we continue on this note? I have run into it again, and would like some advice.

I want to have a public subroutine that will set certain criteria on each form, such as displaying a logo, the background color, the company name, etc. Each form should have the same look and feel to it.

But, if I change something, I don't want to go to every form and make the change. I want to change, for example, the background color in the subroutine, and it will apply it once. The subroutine would be called in each form's load event.

In order to do this, wouldn't I have to pass the form name to the subroutine? Or am I just not seeing how to do it correctly?

Would it be better to store these variables in a file, and just have each form access them as it loads?

Any help and comments are welcome (even if it is constructive critisism!)
 
cassandra and strongm were pointing you in the right direction, passing a reference to the form itself:
Code:
private sub AnalyzeRecord(byref frmName as form)
frmName.[i]attribute[/i] = [i]value[/i]
Code:
AnalyzeRecord Me
Adding byref may be important, otherwise you might be trying to pass the default attribute of Me, which is probably the form's name, not a reference to the form itself.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top