Go Back   Access World Forums > Microsoft Access Reference > Code Repository

 
Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 5.00 average. Display Modes
Old 07-09-2009, 12:17 AM   #1
Guus2005
AWF VIP
 
Guus2005's Avatar
 
Join Date: Jun 2007
Location: The Netherlands
Posts: 2,521
Thanks: 53
Thanked 84 Times in 77 Posts
Guus2005 has a spectacular aura about Guus2005 has a spectacular aura about Guus2005 has a spectacular aura about
Thumbs up New, faster GetPart function without recursion using Split()

This is a faster GetPart function using the Split() command.

Code:
Public Function GetPart(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006/2007
'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_GetPart

    arr = Split(strString, strSep)
    'get number of items in the array
    intHigh = UBound(arr) + 1
    
    If intHigh = 0 Then 'No items in array
        GetPart = strString
        Exit Function
    End If
    
    If intPart = 0 Then
        GetPart = strString
        Exit Function
    ElseIf intPart > intHigh Then
        intPart = intHigh  'Get the last item
    End If
    
    GetPart = Trim(arr(intPart - 1))

Exit_GetPart:
    Exit Function

Err_GetPart:
    MsgBox Err & " - " & Error$, vbExclamation, "Getpart function"
    Resume Exit_GetPart
Resume
End Function
Enjoy!

Guus2005 is offline   Reply With Quote
Old 06-14-2012, 02:38 AM   #2
Guus2005
AWF VIP
 
Guus2005's Avatar
 
Join Date: Jun 2007
Location: The Netherlands
Posts: 2,521
Thanks: 53
Thanked 84 Times in 77 Posts
Guus2005 has a spectacular aura about Guus2005 has a spectacular aura about Guus2005 has a spectacular aura about
Re: New, faster GetPart function without recursion using Split()

Or slightly simpler without all the checks
Code:
Public Function GetPart2(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006/2012
'Getpart with no checks. If you are sure the parameters are ok.

    GetPart2 = Split(strString, strSep)(intPart - 1)

End Function
Share & Enjoy!
Guus2005 is offline   Reply With Quote
Old 06-14-2012, 02:39 AM   #3
Guus2005
AWF VIP
 
Guus2005's Avatar
 
Join Date: Jun 2007
Location: The Netherlands
Posts: 2,521
Thanks: 53
Thanked 84 Times in 77 Posts
Guus2005 has a spectacular aura about Guus2005 has a spectacular aura about Guus2005 has a spectacular aura about
Re: New, faster GetPart function without recursion using Split()

When you are sure all parameters are okay, you can use this version.

Code:
Public Function GetPart2(strString As String, strSep As String, intPart As Integer) As String
'Guus2005 - 2006/2012
'Getpart with no checks. If you are sure the parameters are ok.

    GetPart2 = Split(strString, strSep)(intPart - 1)

End Function
Share & Enjoy!

Guus2005 is offline   Reply With Quote
Reply

Tags
getpart , split

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed Issue hey23 General 7 06-25-2009 01:33 AM
Event when calculation is completed? fugifox Forms 7 02-03-2009 12:17 AM
Search the users registry for a specific key ghudson General 5 06-15-2005 05:21 AM
need to make a custom function to split a sentence into words Atomic Shrimp Modules & VBA 4 12-21-2000 12:11 PM




All times are GMT -8. The time now is 05:36 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World