AntonioPS
Oct 23 2007, 11:38 AM
I like to try and call functions where i can for individual processes. OOP?
How can i pass a value from where the function is being called to the actual function
ie
Sub
call fn
myvalue= .value
end sub
function
use.value in code
MattJ
Oct 23 2007, 11:43 AM
An example function to add two numbers together;
Function AddNums(intNum1 as Integer, intNum2 as Integer)
AddNums = intNum1 + intNum2)
End Function
Notice the arguments for the function in the first line. Then to call this function, it would be
AddNums(1,2)
The result would be 3. You wouldn't have to directly enter numbers, it also could be a variable or a form reference.
Larry Larsen
Oct 23 2007, 11:49 AM
Hi
Matt missed of a "(" from the function..
CODE
Function AddNums(intNum1 as Integer, intNum2 as Integer)
AddNums = [color="red"]([/color]intNum1 + intNum2)
End Function
HTH's
AntonioPS
Oct 23 2007, 11:50 AM
hmm maybe i am using the wrong terminology.
so there are public and private subs as well as public and private functions. or at least that is what my book tells me.
i want to pass a variable from a sub to a function?
or do i have to declare the variable again in the function and look up the value i want again from a form?
MattJ
Oct 23 2007, 11:55 AM
You could declare a global variable - then you could set it and retrieve the value from anywhere. Usually in functions, the variables that are passed are arguments to the function (as shown above).
AntonioPS
Oct 23 2007, 12:03 PM
ahh i see. sorry had to look up global variable.
I would place this at the top
Option Compare Database
Public myvariable As String
Option Explicit
In a sub i would first set "myvalue" = nothing then give it a value and call the function
Then in the Function i would use it by simply using myvalue in my code
is that correct?
MattJ
Oct 23 2007, 12:05 PM
It depends on the function, but yes...
AntonioPS
Oct 23 2007, 12:08 PM
am i missing something? when should i be using a sub versus a function?
I am reading your response as if i am just not getting it.
jasonlewis
Oct 23 2007, 12:42 PM
Hi Antonio,
1) The general difference between a procedure (Sub) and a function (Function) is that a function is conventionally supposed to return a value, where a procedure cannot return a value.
2) As to passing and updating variables from within a function or procedure, it depends on how you pass them. By default, variables passed as arguments to functions can be modified (e.g. they are passed by reference).
CODE
[color="green"]' A Procedure To Increment A Value Passed As An Argument[/color]
Public Sub MyIncrementProc(Value As Integer)
Value = Value + 1
End Sub
[color="green"]' A Function To Increment A Value Passed As An Argument[/color]
Public Function MyIncrementFunc(Value As Integer) As Boolean
On Error Goto Err_MyIncrementFunc
Value = Value + 1
MyIncrementFunc = True
Exit_MyIncrementFunc:
Exit Function
Err_MyIncrementFunc:
MsgBox "MyIncrmementFunc ERROR" & Err.Description
Resume Exit_MyIncrementFunc
End Function
[color="green"]' A Function To Increment A Value Passed As An Argument (By Value)[/color]
Public Function MyOtherIncrementFunc(ByVal Value As Integer) As Integer
On Error Goto Err_MyOtherIncrementFunc
MyOtherIncrementFunc = Value + 1
Value = Value + 100 '<--this won't do anything to callers variable Value because it is passed in ByVal not ByRef (default)
Exit_MyOtherIncrementFunc:
Exit Function
Err_MyOtherIncrementFunc:
MsgBox "MyOtherIncrmementFunc ERROR" & Err.Description
Resume Exit_MyIncrementFunc
End Function
Public Sub Test()
Dim iValue As Integer
Dim iTemp As Integer
iValue = 1
Debug.Print "iValue=[" & iValue & "]"
Call MyIncrementProc(iValue)
Debug.Print "iValue (after increment procedure)=[" & iValue & "]"
If MyIncrementFunc(iValue) Then
Debug.Print "iValue (after increment function)=[" & iValue & "]"
End If
iTemp = MyOtherIncrementFunc(iValue)
Debug.Print "iValue (after other increment function)=[" & iValue & "]"
Debug.Print "iTemp (after other increment function)=[" & iTemp & "]"
[/color]
End Sub
Hope this is useful,
Jason
MattJ
Oct 23 2007, 12:43 PM
The general difference between sub and functions is that functions produce a value. Subs merely process lines of code.
AntonioPS
Oct 23 2007, 01:03 PM
Oh wow okay I really get it now.
I should probably rename my functions as subs then because they do not produce a value.
Thank you Matt and Jason!
Jason- after your procedure name you fill things in the ( )
what is that called? the are in between the brackets
Public Function MyOtherIncrementFunc(ByVal Value As Integer) As Integer
i want to look it up. up until now i havent used it myself
jasonlewis
Oct 23 2007, 01:14 PM
Hi Antonio,
The items within the parenthesis of a Function or Procedure are called arguments (sometimes they are also called parameters).
In the Office Online help you can search on 'About procedures' and find the reference on syntax.
Hope this helps,
Jason
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.