View Full Version : Get the n-th string faster


Guus2005
04-28-2008, 04:21 AM
This new function runs faster than this (http://www.access-programmers.co.uk/forums/showthread.php?t=130858)one
Return the n-th string using a separator.
Public Function GetPart2(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006
'New GetPart function without recursion, returns part "intPart" of "strString" with "strSep" as a separator
'Uses an array and the Split() function.
Dim arr() As String
Dim intHigh As Integer

On Error GoTo Err_GetPart2

arr = Split(strString, strSep)
'get number of items in the array
intHigh = UBound(arr)

If intPart <= 0 Then
intPart = 1 'Get the first item
ElseIf intPart >= intHigh Then
intPart = intHigh + 1 'Get the last item
End If

GetPart2 = arr(intPart - 1)

Exit_GetPart2:
Exit Function

Err_GetPart2:
msgbox Err & " " & Error$, "GetPart2",
Resume Exit_GetPart2
End Function
Enjoy!