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 multiply parameters to a function in a module

Status
Not open for further replies.

don2241

IS-IT--Management
Jun 1, 2001
57
AU
Hi!
I'm trying to pass many parameters to a function, this is a snipplet of my code:

Dim prod as Integer
Dim sub1 as Integer
Dim sub2 as Integer

prod = 1
sub1 = 2
sub2 = 3


'I get my error message on this line saying: " = Expected"
myFunction (prod, sub1, sub2)


And my code in my function looks like this:


Public function myFunction (p as Integer, s1 as Integer, s2 as Integer)

Dim pr as Integer
Dim su1 as Integer
Dim su2 as Integer

pr = p
su1 = s1
su2 = s2

End Function

Thank you for any help
 
Don,

A function by definition returns a result. for example, a simple function to add two numbers might look like this:

function AddTwoNumbers(a, b)
AddTwoNumbers = a + b
end function

Then to call this function, one might have

FirstNumber = 3
SecondNumber = 4
SumAB = AddTwoNumbers(FirstNumber, SecondNumber)

The reason you're getting your problem is that you're calling the function without assigning its return result to anything.

Simply changing your statement:

myFunction (prod, sub1, sub2)

to

Dim Dummy
Dummy = myFunction (prod, sub1, sub2)

will fix the problem, as the value of the function will be assigned to the variable Dummy, which whilst it doesnt do anything, is syntactically correct.

Where you wish to define a routine without returning a value, use a Sub instead of a Function. Check online help for further info; main difference is that you dont need to assign the value of a Sub to anything when you invoke it (as it has no return value

I hope this helps,





Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
You don't need to assign a function call to anything in order to use it. If you don't put brackets in the expression it executes without trying to assign the value :
Code:
myFunction prod, sub1, sub2
Access built-in functions work the same way. You can use MsgBox to show a message or to return a value depending on whether you use brackets or not :
Code:
MsgBox "Missing description", vbOkOnly, "Error"
-or- 
If MsgBox("Abandon changes to this record?", vbYesNo +      vbQuestion, "Confirm Action") = vbYes Then
  Me.Undo
  DoCmd.Close
End If
I In my last contract I was told to define everything as a function. Even if they didn't return anything you had to return a True/False value to indicate whether the function completed successfully. If you don't use the return value it's no big deal. PeteJ
(Contract Code-monkey)

It's amazing how many ways there are to skin a cat
(apologies to the veggies)
 
Pete,

I stand corrected. When using the fuction, I have always used the brackets, and where applicable, assigned the return value to a dummy variable. Old habits will die hard, but thanks for the correction,

Regards Steve Lewy
Solutions Developer
steve@lewycomputing.com.au
(dont cut corners or you'll go round in circles)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top