return a value from a function
Author |
Message |
Trond R #1 / 8
|
 return a value from a function
I'm trying to write a function that accepts two string arguments. The function should manipulate the strings, and then return a single string. Is this possible? I'm using Access 2000. My sample (non-working) code: **** Code start **** Dim strNew as String 'strNew should not be public strNew = manipulateStrings(str1, str2) private sub manipulateStrings(strArg1 as String, strArg2 as String) 'Since I don't want to manipulate strNew directly here, I'll create new temporary string. Dim strTemp as String 'Manipulate strings and save result as strTemp, e.g strTemp = "A string based on the given arguments " & strArg1 & " and " & strArg2 return strTemp end sub **** Code end **** Is what I'm trying to do possible? If it is, please explain how! Thanks in advance for answering. Regards TRB
|
Mon, 19 Jan 2004 16:07:24 GMT |
|
 |
Brendan Reynold #2 / 8
|
 return a value from a function
You're almost there. I only see two obvious problems. First, your procedure is a sub, and to return a value it needs to be a function. Change 'Private Sub' to 'Private Function' (unless you want to be able to call it from modules other than the one in which it is declared, in which case make it 'Public Function'). As it is to return a string, you should also specify the return type of the function, otherwise it will default to Variant. So - "Private Function ManipulateStrings(strArg1 As String, strArg2 As String) As String". Also change the 'End Sub' to 'End Function' (actually, I think the VBA IDE does that automatically for you when you change the first line). Second, to assign the result of the expression to the return value of the function, instead of 'return strTemp' you need "ManipulateStrings = strTemp". -- Brendan Reynolds
Quote: > I'm trying to write a function that accepts two string arguments. The > function should manipulate the strings, and then return a single string. Is > this possible? I'm using Access 2000. > My sample (non-working) code: > **** Code start **** > Dim strNew as String 'strNew should not be public > strNew = manipulateStrings(str1, str2) > private sub manipulateStrings(strArg1 as String, strArg2 as String) > 'Since I don't want to manipulate strNew directly here, I'll create > new temporary string. > Dim strTemp as String > 'Manipulate strings and save result as strTemp, e.g > strTemp = "A string based on the given arguments " & strArg1 & " and > " & strArg2 > return strTemp > end sub > **** Code end **** > Is what I'm trying to do possible? If it is, please explain how! > Thanks in advance for answering. > Regards TRB
|
Mon, 19 Jan 2004 18:09:49 GMT |
|
 |
Trond R #3 / 8
|
 return a value from a function
Thanks Brendan. That solved the problem! Regards TRB
Quote: > You're almost there. I only see two obvious problems. > First, your procedure is a sub, and to return a value it needs to be a > function. Change 'Private Sub' to 'Private Function' (unless you want to be > able to call it from modules other than the one in which it is declared, in > which case make it 'Public Function'). As it is to return a string, you > should also specify the return type of the function, otherwise it will > default to Variant. So - "Private Function ManipulateStrings(strArg1 As > String, strArg2 As String) As String". Also change the 'End Sub' to 'End > Function' (actually, I think the VBA IDE does that automatically for you > when you change the first line). > Second, to assign the result of the expression to the return value of the > function, instead of 'return strTemp' you need "ManipulateStrings = > strTemp". > -- > Brendan Reynolds
> > I'm trying to write a function that accepts two string arguments. The > > function should manipulate the strings, and then return a single string. > Is > > this possible? I'm using Access 2000. > > My sample (non-working) code: > > **** Code start **** > > Dim strNew as String 'strNew should not be public > > strNew = manipulateStrings(str1, str2) > > private sub manipulateStrings(strArg1 as String, strArg2 as String) > > 'Since I don't want to manipulate strNew directly here, I'll > create > > new temporary string. > > Dim strTemp as String > > 'Manipulate strings and save result as strTemp, e.g > > strTemp = "A string based on the given arguments " & strArg1 & " > and > > " & strArg2 > > return strTemp > > end sub > > **** Code end **** > > Is what I'm trying to do possible? If it is, please explain how! > > Thanks in advance for answering. > > Regards TRB
|
Mon, 19 Jan 2004 18:26:25 GMT |
|
 |
Trond R #4 / 8
|
 return a value from a function
I'm wondering how to use the function if I put it in a module, say 'myUtils'. How would i refer to it, and how would i get the variable 'strNew' set? Regards TRB
Quote: > Thanks Brendan. That solved the problem! > Regards TRB
> > You're almost there. I only see two obvious problems. > > First, your procedure is a sub, and to return a value it needs to be a > > function. Change 'Private Sub' to 'Private Function' (unless you want to > be > > able to call it from modules other than the one in which it is declared, > in > > which case make it 'Public Function'). As it is to return a string, you > > should also specify the return type of the function, otherwise it will > > default to Variant. So - "Private Function ManipulateStrings(strArg1 As > > String, strArg2 As String) As String". Also change the 'End Sub' to 'End > > Function' (actually, I think the VBA IDE does that automatically for you > > when you change the first line). > > Second, to assign the result of the expression to the return value of the > > function, instead of 'return strTemp' you need "ManipulateStrings = > > strTemp". > > -- > > Brendan Reynolds
> > > I'm trying to write a function that accepts two string arguments. The > > > function should manipulate the strings, and then return a single string. > > Is > > > this possible? I'm using Access 2000. > > > My sample (non-working) code: > > > **** Code start **** > > > Dim strNew as String 'strNew should not be public > > > strNew = manipulateStrings(str1, str2) > > > private sub manipulateStrings(strArg1 as String, strArg2 as String) > > > 'Since I don't want to manipulate strNew directly here, I'll > > create > > > new temporary string. > > > Dim strTemp as String > > > 'Manipulate strings and save result as strTemp, e.g > > > strTemp = "A string based on the given arguments " & strArg1 & " > > and > > > " & strArg2 > > > return strTemp > > > end sub > > > **** Code end **** > > > Is what I'm trying to do possible? If it is, please explain how! > > > Thanks in advance for answering. > > > Regards TRB
|
Mon, 19 Jan 2004 20:55:22 GMT |
|
 |
Brendan Reynold #5 / 8
|
 return a value from a function
Well, as I said in my first reply, if you want to be able to use the function from outside the module in which it is declared, you need to use "Public Function" instead of "Private Function". Other than that, you use it exactly as you showed in your original post: "strNew = manipulateStrings(str1, str2)" -- Brendan Reynolds
http://www11.ewebcity.com/brenreyn
Quote: > I'm wondering how to use the function if I put it in a module, say > 'myUtils'. > How would i refer to it, and how would i get the variable 'strNew' set? > Regards TRB
> > Thanks Brendan. That solved the problem! > > Regards TRB
> > > You're almost there. I only see two obvious problems. > > > First, your procedure is a sub, and to return a value it needs to be a > > > function. Change 'Private Sub' to 'Private Function' (unless you want to > > be > > > able to call it from modules other than the one in which it is declared, > > in > > > which case make it 'Public Function'). As it is to return a string, you > > > should also specify the return type of the function, otherwise it will > > > default to Variant. So - "Private Function ManipulateStrings(strArg1 As > > > String, strArg2 As String) As String". Also change the 'End Sub' to 'End > > > Function' (actually, I think the VBA IDE does that automatically for you > > > when you change the first line). > > > Second, to assign the result of the expression to the return value of > the > > > function, instead of 'return strTemp' you need "ManipulateStrings = > > > strTemp". > > > -- > > > Brendan Reynolds
> > > > I'm trying to write a function that accepts two string arguments. The > > > > function should manipulate the strings, and then return a single > string. > > > Is > > > > this possible? I'm using Access 2000. > > > > My sample (non-working) code: > > > > **** Code start **** > > > > Dim strNew as String 'strNew should not be public > > > > strNew = manipulateStrings(str1, str2) > > > > private sub manipulateStrings(strArg1 as String, strArg2 as String) > > > > 'Since I don't want to manipulate strNew directly here, I'll > > > create > > > > new temporary string. > > > > Dim strTemp as String > > > > 'Manipulate strings and save result as strTemp, e.g > > > > strTemp = "A string based on the given arguments " & strArg1 & > " > > > and > > > > " & strArg2 > > > > return strTemp > > > > end sub > > > > **** Code end **** > > > > Is what I'm trying to do possible? If it is, please explain how! > > > > Thanks in advance for answering. > > > > Regards TRB
|
Tue, 20 Jan 2004 02:13:13 GMT |
|
 |
Miche #6 / 8
|
 return a value from a function
you should use the module name in the function call because if you heve the same function name deffined in two modules you could run into some problems strNew =[ModuleName].manipulateStrings(str1, str2)" Michel Quote: >-----Original Message----- >Well, as I said in my first reply, if you want to be able to use the >function from outside the module in which it is declared, you need to use >"Public Function" instead of "Private Function". Other
than that, you use it Quote: >exactly as you showed in your original post: "strNew = >manipulateStrings(str1, str2)" >-- >Brendan Reynolds
>http://www11.ewebcity.com/brenreyn
>> I'm wondering how to use the function if I put it in a module, say >> 'myUtils'. >> How would i refer to it, and how would i get the
variable 'strNew' set? Quote: >> Regards TRB
>> > Thanks Brendan. That solved the problem! >> > Regards TRB
message
>> > > You're almost there. I only see two obvious problems. >> > > First, your procedure is a sub, and to return a
value it needs to be a Quote: >> > > function. Change 'Private Sub' to 'Private
Function' (unless you want Quote: >to >> > be >> > > able to call it from modules other than the one in which it is >declared, >> > in >> > > which case make it 'Public Function'). As it is to return a string, >you >> > > should also specify the return type of the
function, otherwise it will Quote: >> > > default to Variant. So - "Private Function
ManipulateStrings(strArg1 Quote: >As >> > > String, strArg2 As String) As String". Also change the 'End Sub' to >'End >> > > Function' (actually, I think the VBA IDE does that automatically for >you >> > > when you change the first line). >> > > Second, to assign the result of the expression to the return value of >> the >> > > function, instead of 'return strTemp' you
need "ManipulateStrings = Quote: >> > > strTemp". >> > > -- >> > > Brendan Reynolds
>> > > > I'm trying to write a function that accepts two string arguments. >The >> > > > function should manipulate the strings, and then return a single >> string. >> > > Is >> > > > this possible? I'm using Access 2000. >> > > > My sample (non-working) code: >> > > > **** Code start **** >> > > > Dim strNew as String 'strNew should not be public >> > > > strNew = manipulateStrings(str1, str2) >> > > > private sub manipulateStrings(strArg1 as String, strArg2 as String) >> > > > 'Since I don't want to manipulate strNew directly here, I'll >> > > create >> > > > new temporary string. >> > > > Dim strTemp as String >> > > > 'Manipulate strings and save result as strTemp, e.g >> > > > strTemp = "A string based on the given
arguments " & strArg1 Quote: >& >> " >> > > and >> > > > " & strArg2 >> > > > return strTemp >> > > > end sub >> > > > **** Code end **** >> > > > Is what I'm trying to do possible? If it is, please explain how! >> > > > Thanks in advance for answering. >> > > > Regards TRB >.
|
Tue, 20 Jan 2004 02:42:24 GMT |
|
 |
Brendan Reynold #7 / 8
|
 return a value from a function
Well, yes. Personally I just wouldn't *have* two public procedures with the same name, but if someone did, then yes, it would be necessary to 'disambiguate' by including the module name in the procedure call. -- Brendan Reynolds
http://www11.ewebcity.com/brenreyn
Quote: > you should use the module name in the function call > because if you heve the same function name deffined in two > modules you could run into some problems > strNew =[ModuleName].manipulateStrings(str1, str2)" > Michel > >-----Original Message----- > >Well, as I said in my first reply, if you want to be able > to use the > >function from outside the module in which it is declared, > you need to use > >"Public Function" instead of "Private Function". Other > than that, you use it > >exactly as you showed in your original post: "strNew = > >manipulateStrings(str1, str2)" > >-- > >Brendan Reynolds
> >http://www11.ewebcity.com/brenreyn
> >> I'm wondering how to use the function if I put it in a > module, say > >> 'myUtils'. > >> How would i refer to it, and how would i get the > variable 'strNew' set? > >> Regards TRB
> >> > Thanks Brendan. That solved the problem! > >> > Regards TRB
> message
> >> > > You're almost there. I only see two obvious > problems. > >> > > First, your procedure is a sub, and to return a > value it needs to be a > >> > > function. Change 'Private Sub' to 'Private > Function' (unless you want > >to > >> > be > >> > > able to call it from modules other than the one in > which it is > >declared, > >> > in > >> > > which case make it 'Public Function'). As it is to > return a string, > >you > >> > > should also specify the return type of the > function, otherwise it will > >> > > default to Variant. So - "Private Function > ManipulateStrings(strArg1 > >As > >> > > String, strArg2 As String) As String". Also change > the 'End Sub' to > >'End > >> > > Function' (actually, I think the VBA IDE does that > automatically for > >you > >> > > when you change the first line). > >> > > Second, to assign the result of the expression to > the return value of > >> the > >> > > function, instead of 'return strTemp' you > need "ManipulateStrings = > >> > > strTemp". > >> > > -- > >> > > Brendan Reynolds
> >> > > > I'm trying to write a function that accepts two > string arguments. > >The > >> > > > function should manipulate the strings, and then > return a single > >> string. > >> > > Is > >> > > > this possible? I'm using Access 2000. > >> > > > My sample (non-working) code: > >> > > > **** Code start **** > >> > > > Dim strNew as String 'strNew should not be public > >> > > > strNew = manipulateStrings(str1, str2) > >> > > > private sub manipulateStrings(strArg1 as String, > strArg2 as String) > >> > > > 'Since I don't want to manipulate strNew > directly here, I'll > >> > > create > >> > > > new temporary string. > >> > > > Dim strTemp as String > >> > > > 'Manipulate strings and save result as > strTemp, e.g > >> > > > strTemp = "A string based on the given > arguments " & strArg1 > >& > >> " > >> > > and > >> > > > " & strArg2 > >> > > > return strTemp > >> > > > end sub > >> > > > **** Code end **** > >> > > > Is what I'm trying to do possible? If it is, > please explain how! > >> > > > Thanks in advance for answering. > >> > > > Regards TRB > >.
|
Tue, 20 Jan 2004 05:52:02 GMT |
|
 |
Trond R #8 / 8
|
 return a value from a function
Thanks both of you. You're great helpers! Regards TRB
Quote: > Well, yes. Personally I just wouldn't *have* two public procedures with the > same name, but if someone did, then yes, it would be necessary to > 'disambiguate' by including the module name in the procedure call. > -- > Brendan Reynolds
> http://www11.ewebcity.com/brenreyn
> > you should use the module name in the function call > > because if you heve the same function name deffined in two > > modules you could run into some problems > > strNew =[ModuleName].manipulateStrings(str1, str2)" > > Michel > > >-----Original Message----- > > >Well, as I said in my first reply, if you want to be able > > to use the > > >function from outside the module in which it is declared, > > you need to use > > >"Public Function" instead of "Private Function". Other > > than that, you use it > > >exactly as you showed in your original post: "strNew = > > >manipulateStrings(str1, str2)" > > >-- > > >Brendan Reynolds
> > >http://www11.ewebcity.com/brenreyn
> > >> I'm wondering how to use the function if I put it in a > > module, say > > >> 'myUtils'. > > >> How would i refer to it, and how would i get the > > variable 'strNew' set? > > >> Regards TRB
> > >> > Thanks Brendan. That solved the problem! > > >> > Regards TRB
> > message
> > >> > > You're almost there. I only see two obvious > > problems. > > >> > > First, your procedure is a sub, and to return a > > value it needs to be a > > >> > > function. Change 'Private Sub' to 'Private > > Function' (unless you want > > >to > > >> > be > > >> > > able to call it from modules other than the one in > > which it is > > >declared, > > >> > in > > >> > > which case make it 'Public Function'). As it is to > > return a string, > > >you > > >> > > should also specify the return type of the > > function, otherwise it will > > >> > > default to Variant. So - "Private Function > > ManipulateStrings(strArg1 > > >As > > >> > > String, strArg2 As String) As String". Also change > > the 'End Sub' to > > >'End > > >> > > Function' (actually, I think the VBA IDE does that > > automatically for > > >you > > >> > > when you change the first line). > > >> > > Second, to assign the result of the expression to > > the return value of > > >> the > > >> > > function, instead of 'return strTemp' you > > need "ManipulateStrings = > > >> > > strTemp". > > >> > > -- > > >> > > Brendan Reynolds
> > >> > > > I'm trying to write a function that accepts two > > string arguments. > > >The > > >> > > > function should manipulate the strings, and then > > return a single > > >> string. > > >> > > Is > > >> > > > this possible? I'm using Access 2000. > > >> > > > My sample (non-working) code: > > >> > > > **** Code start **** > > >> > > > Dim strNew as String 'strNew should not be public > > >> > > > strNew = manipulateStrings(str1, str2) > > >> > > > private sub manipulateStrings(strArg1 as String, > > strArg2 as String) > > >> > > > 'Since I don't want to manipulate strNew > > directly here, I'll > > >> > > create > > >> > > > new temporary string. > > >> > > > Dim strTemp as String > > >> > > > 'Manipulate strings and save result as > > strTemp, e.g > > >> > > > strTemp = "A string based on the given > > arguments " & strArg1 > > >& > > >> " > > >> > > and > > >> > > > " & strArg2 > > >> > > > return strTemp > > >> > > > end sub > > >> > > > **** Code end **** > > >> > > > Is what I'm trying to do possible? If it is, > > please explain how! > > >> > > > Thanks in advance for answering. > > >> > > > Regards TRB > > >.
|
Tue, 20 Jan 2004 15:32:52 GMT |
|
|
|