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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing values to a function.

Status
Not open for further replies.

purephase

Technical User
Oct 23, 2003
13
CA
I know the answer is probably simple, but for some reason I can't figure this out.

Option Explicit
Dim arrParts

arrParts = array("1","2","3","4","5")

objReturn = Query("Object", arrParts(0))

Function Query(strType, strName)
' Execute function commands
Query = objReturn
End Function

When run, it errors out with 800A01C2 "Wrong number of arguments or invalid property assignment", but I don't see how it's possible since there are two arguments passed and received.

Any help would be appreciated. Thanks.
 
Maybe it's getting confused by the use of "objReturn" inside the function and assigning the value to it when the function is called...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Isn't that how you're supposed to return values from functions in vbscript?
 
This works for me....

arrParts = array("1","2","3","4","5")

objReturn = Query("Object", arrParts(0))

Function Query(strType, strName)
' Execute function commands
Query = strType & strName
End Function

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Hmm.. that still results in the same error for me.

This is strange. Thanks for the help though.
 
Nevermind. I figured it out. In the actual function, the returned value is an object, and it's incorrectly defined when the function is called.

It should read:

Set objReturn = Query("Object", arrParts(0))
..
Query = objReturn

This works, the only problem is that the attributes assigned to the object in the function don't seem to apply once it's been returned.

In this instance, I assign objReturn to be a ADODB recordset. Once I return the object, I try to reference objReturn.RecordCount and it results in a message stating that the object doesn't support this property or method.
 
Alright, figured that out too.

So, I guess this turns into a tip of some sort. How to correctly pass a value, or array value into a function, process it and initiate an object, and correctly return that object.

See below:

Option Explicit
Dim arrParts

arrParts = array("1","2","3","4","5")

Set objReturn = Query("Object", arrParts(0))
MsgBox objReturn.<Property>

Function Query(strType, strName)
Set objReturn = GetObject(..) ' Initialize the object.
Set Query = objReturn
End Function

Probably countless tutorial out there that describe how to do this, but hey.. one more can't hurt.

;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top