469,643 Members | 1,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

Function returning array of strings

Hi

I need to return an array of string in my own split function (access 97). I
have defined the function as below but I get err on 'As String()'. What can
I do to make the function return an array of strings?

Public Function Split(ByVal strIn As String, Optional strDelimiter As String
= " ") As String()

Thanks

Regards
Jan 29 '06 #1
4 14581
John wrote in message <O5*************@tk2msftngp13.phx.gbl> :
Hi

I need to return an array of string in my own split function (access 97). I
have defined the function as below but I get err on 'As String()'. What can I
do to make the function return an array of strings?

Public Function Split(ByVal strIn As String, Optional strDelimiter As String
= " ") As String()

Thanks

Regards


Yes, I'm afraid that returning arrays from functions, as far as I've
understood, is a feature included in later versions (2000 and later).

In a97, I think one will need to return a variant.

Here's a function I wrote/adapted some time ago, which can perhaps be a
starting point. I wrote this for a specific purpose, so I don't know
whether it will deal with all possible variations. For instance, it
will
not accept Null (string declaration) ...

Public Function rvsSplit(ByVal v_strInString As String, _
Optional ByVal v_strDelimiter As String = "|")
As Variant
' royvidar
' created 2005-03-09
' purpose: split a string into a variant array for processing
' In this setting, I relax a little on testing, as I'll
' only pass string variables. Use variant and add a test
' with the IsMissing function to use in other context
' parameters:
' v_strInString - string containing text with delimiter
' i e - string to be split
' v_strDelimiter - the delimiter to use in the split
' returns: variant array

Dim lngCounter As Long ' count number of delimiters to redim
array
Dim lngStart As Long ' start position of string to extract
Dim lngStop As Long ' end postition of string to extract
Dim varResult() ' variant array assigned as return value

On Error GoTo rvsSplit_Err

If Len(v_strInString) > 0 Then
lngStart = 1
Do
lngStop = InStr(lngStart, v_strInString, v_strDelimiter)
If lngStop = 0 Then Exit Do
ReDim Preserve varResult(lngCounter)
varResult(lngCounter) = _
Mid$(v_strInString, lngStart, lngStop - lngStart)
lngCounter = lngCounter + 1
lngStart = lngStop + Len(v_strDelimiter)
Loop
ReDim Preserve varResult(lngCounter)
varResult(lngCounter) = Mid$(v_strInString, lngStart)
Else
rvsSplit = Array()
End If
rvsSplit = varResult

rvsSplit_Exit:
Exit Function
rvsSplit_Err:
rvsSplit = vbNullString
Resume rvsSplit_Exit
End Function

--
Roy-Vidar
Jan 29 '06 #2
On Sun, 29 Jan 2006 09:59:48 -0000, "John" <Jo**@nospam.infovis.co.uk> wrote:
Hi

I need to return an array of string in my own split function (access 97). I
have defined the function as below but I get err on 'As String()'. What can
I do to make the function return an array of strings?

Public Function Split(ByVal strIn As String, Optional strDelimiter As String
= " ") As String()

Thanks

Regards


Remove the () after As String.
Public Function Split(ByVal strIn As String, Optional strDelimiter As String = " ") As String
Wayne Gillespie
Gosford NSW Australia
Jan 29 '06 #3
On Sun, 29 Jan 2006 10:34:17 GMT, Wayne Gillespie <be*****@NOhotmailSPAM.com.au> wrote:
On Sun, 29 Jan 2006 09:59:48 -0000, "John" <Jo**@nospam.infovis.co.uk> wrote:
Hi

I need to return an array of string in my own split function (access 97). I
have defined the function as below but I get err on 'As String()'. What can
I do to make the function return an array of strings?

Public Function Split(ByVal strIn As String, Optional strDelimiter As String
= " ") As String()

Thanks

Regards


Remove the () after As String.
Public Function Split(ByVal strIn As String, Optional strDelimiter As String = " ") As String

Forget that. I misread the part about returning a string array. As Roy says this isn't possible in A97 AFAIK.
Wayne Gillespie
Gosford NSW Australia
Jan 29 '06 #4
As the others have indicated, Access 97 doesn't let you return a string
array, but you can pass an array as a variant without any problems.

I just tested the following code in Access 97:

Function ReturnArray() As Variant

Dim strArray(1 To 3) As String

strArray(1) = "One"
strArray(2) = "Two"
strArray(3) = "Three"

ReturnArray = strArray

End Function

Sub CallArray()

Dim intLoop As Integer
Dim varReturn As Variant

varReturn = ReturnArray()
For intLoop = LBound(varReturn) To UBound(varReturn)
Debug.Print intLoop & ": " & varReturn(intLoop)
Next intLoop

End Sub

In the Immediate window:

CallArray
1: One
2: Two
3: Three

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)
"John" <Jo**@nospam.infovis.co.uk> wrote in message
news:O5*************@tk2msftngp13.phx.gbl...
Hi

I need to return an array of string in my own split function (access 97).
I have defined the function as below but I get err on 'As String()'. What
can I do to make the function return an array of strings?

Public Function Split(ByVal strIn As String, Optional strDelimiter As
String = " ") As String()

Thanks

Regards

Jan 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Tweaxor | last post: by
5 posts views Thread by damian birchler | last post: by
4 posts views Thread by Woody Splawn | last post: by
5 posts views Thread by shyam | last post: by
18 posts views Thread by svata | last post: by
20 posts views Thread by Andrew Morton | last post: by
8 posts views Thread by jodleren | last post: by
26 posts views Thread by aruna.mysore | last post: by
13 posts views Thread by Sri Harsha Dandibhotla | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.