Question in regards to function

hbrems

has no clue...
Local time
Tomorrow, 00:27
Joined
Nov 2, 2006
Messages
181
Hi,

I just got a simple question in regards to functions. For example this one:

Code:
Public Function writetotext(source, destination As String)

' writes source to textfile

Const ForAppending = 8, ForReading = 1, ForWriting = 2
Dim fs, f

' create systemobject
Set fs = CreateObject("Scripting.FileSystemObject")

' open file and write line
Set f = fs.OpenTextFile(destination, ForAppending)
f.Writeline source

' close file
f.Close

End Function

When I call this function from my form, why does it require me to type an equal sign like this:

Code:
writeit = writetotext(me.txttest, "C:\test.txt")

instead of just typing this:

Code:
writetotext(me.txttest, "C:\test.txt")

It has something to do with the parameters but I'm probably doing something wrong.
 
Don't use brackets. They're only required if your procedure call returns a value, which yours does not. And that being the case, you might rather use...
Code:
Public Sub WriteToText(src as string, dst as string)
 
Hi -

From the Help File -

Sub procedures perform an operation or series of operations but don't return a value.

Function procedures (often simply called functions) return a value, such as the result of a calculation.

**********************************************************

Your example is a sub (erroneously coded as a function)--it performs an action but doesn't return a value.

The calling procedure for each is different.

writeit = writetotext(me.txttest, "C:\test.txt") is the workable calling procedure for a function.

writetotext(me.txttest, "C:\test.txt")
- or -
call writetotext(me.txttest, "C:\test.txt")

are what you'd use to call a sub.

Convert your function to a sub or live with the function calling procedure. It's your call.

HTH - Bob
 
Last edited:
Lagbolt -

Don't use brackets.

May be going blind in my old age, but I can't spot a single bracket [] in the original post.

Help me out here.

Best wishes - Bob
 
A rose by any other name... :rolleyes:
 
Bob…

{
writetotext(me.txttest, "C:\test.txt")
- or -
call writetotext(me.txttest, "C:\test.txt")

are what you'd use to call a sub.
}

I doubt if the above is correct Bob.

If writetotext is a Sub then the editor should put a space between writetotext and (me.txttest, "C:\test.txt") as in

Writetotext (me.txttest, "C:\test.txt")

and that should cause an argument evaluation error because '(me.txttest, "C:\test.txt")'
can not be evaluated to a single argument before being passed to the Sub as a forced pass by value.

Regards,
Chris.
 
Chris

Guess I'll have to quote Lagbolt:

"A rose by any other name"

I don't have a clue of what you're talking about.

Best wishes (I think) - Bob
 
In this thread Bob (7-November-2006): -

http://www.access-programmers.co.uk/forums/showthread.php?t=117262

you were demeaning with your response: -

---
Hi Boys and Girls -

How do we do dates in Access? Do we use Quotes "" or hashmarks ##? ChrisO -- "40 April". Please tell us about it.

Best Wishes, Bob
---

I posted a reply, as everyone can see, yet you choose not to respond.

I guess the logical question now is Bob; are you a Boy or a Girl?
 
Chris -

Hope you haven't been stewing about that all this time (606 days +/-). Didn't realize a reply was called for. My bad.

If I may comment here, it was a well thought out and well documented response.

Have a great day -- Bob
 
It was not your failure to respond that caught my eye, that sort of thing happens a lot of times, it was your opening remark in your posting: -

Hi Boys and Girls –
 
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.

.
 
I got the point a few days ago but still a good read. ;)
 

Users who are viewing this thread

Back
Top Bottom