Hmmmm…..Ya….OK.
In any case…Upon reading the original post, it does indeed contain brackets (parenthesis – a Rose by another name..) as it would be called in some cultures.
Subs and Functions can be used independently or Called by using the Call statement which in essence tells code flow to transfers control to the Sub procedure or Function procedure named. Once the Called procedure is complete, code flow returns again to the code line following the Calling point. Here are some examples:
Let’s assume the WriteToText routine is a Sub Procedure:
WriteToText C:\MySrceTextFile.txt, C:\MyDestTextFile.txt
OR
Call WriteToText(C:\MySrceTextFile.txt, C:\MyDestTextFile.txt)
If Called, parenthesis must be used. If not Called then parenthesis can not be used.
Now…let’s assume the WriteToText routine is a Function Procedure that returns no Value:
WriteToText C:\MySrceTextFile.txt, C:\MyDestTextFile.txt
OR
Call WriteToText(C:\MySrceTextFile.txt, C:\MyDestTextFile.txt)
If Called, parenthesis must be used. If not Called then parenthesis can not be used.
Looks exactly the same doesn’t it….that’s because, it is. The same method applies to both Sub and Function procedures. Then why use Functions you ask, because Function procedures can return a value, Sub procedures can not.
A Function procedure is normally expected to return some value but not necessarily, a Sub procedure does not return a value unless a parameter used within the Sub procedure
has been declared and passed within the routine making the Call to the Sub procedure. Here is a simple example of that:
Place into the OnClick event of a Command Button:
Dim OldParam As String
OldParam = “This is OLD text”
SwapText OldParam, “This is NEW text”
MsgBox OldParam
The Sub Procedure:
Private Sub SwapText(ByVal OldParam As String, ByVal NewParam As String)
OldParam = NewParam
End Sub
Did you notice that the string variable OldParam was declared within the calling routine then passed into the Sub procedure?
To do this with a Function procedure we would merely go:
MsgBox SwapText(“This is OLD text”, “This is NEW text”)
The Function Procedure:
Private Function SwapText(ByVal OldParam As String, ByVal NewParam As String) As String
SwapText = NewParam
End Function
Notice the As String at the end of the Function declaration line. We are declaring that the SwapText function will be returning a String value which in this sample is the contents contained within the NewParam string parameter. Also notice that the value to return is also placed into the Name of the Function procedure. You can sort of think of a Function procedure that returns a specific value as a one shot working Variable.
Generally, all that is needed to remember is that a Function procedure can (if you want) return a value determined by that Function procedure. A Sub procedure can not.
In your case hbrems, the WriteToText procedure could very well be a Sub Procedure but it really doesn’t have to. It is however good practice to do so with a procedure that does not return a value. There are times when this is not desired when working with specific items within Microsoft Access ™.
If for example, you want to utilize a procedure call directly within the event properties box of a Control then is must be applied as a Function procedure. A Sub procedure can not be placed there. For example, let’s assume you have five TextBoxes on a MS Access Form. When someone passes the mouse pointer over any one of the TextBoxes we want a message box to display indicating as such. Instead of entering code into the VBA IDE to call a Sub procedure (or a Function Procedure for that matter) from the MouseMove event to carry out the task you can create a Function procedure to do the job and enter that Function Procedure name directly into event properties box for the MouseMove event, like this:
On Mouse Move …… = MsgMouseOver()
Only Function procedures can be placed directly into a controls event box. Sub procedures can not.
I also always call these ( ) …… brackets.
.