UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> GetListItem    

This function gets a value from a delimited string per the notes below. See usage example in the function header. An example scenario would be when the GetRecordValues function is used to pass a list of arguments to a report or form using the OpenArgs method, this function can then be used to extract specific values from the list.

Essentially, this is just a wrapper function for a common implementation of using the Split() function to break down a list and get a value. This provides a bit of an easier interface (one line called from external procedures rather than the 6 or 7 lines generally required for a traditional string parse/return).


' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' You are free to use this code in any application,
' provided this notice is left unchanged
' NAME: GetListItem
' PURPOSE: Gets the value of an item in a string list
' RETURNS: Variant, value of the item in the list, Null on error/not found
' ARGUMENTS: sList - string to pull from
'            sItem - identifier of the item
'            optional sDelimiter - list delimiter
'   ex:
'   =GetListItem("ITEMTYPE=5;ITEMCODE=1343", "ITEMTYPE")
'   Returns 5
'   Access 97 and earlier require a custom Split() function.
'  R01   8/22/2010    INITIAL
'  R02   8/27/2010    MINOR      Added functionality for values passed either
'                                with or without ";<value>=" wrapper
'ErrHandler V3.01
Public Function GetListItem( _
   sList As String, _
   sItem As String, _
   Optional sDelimiter As String = ";" _
   ) As Variant
On Error GoTo Error_Proc
Dim Ret As Variant
Ret = Null
 Dim v As Variant 'array to hold the items and their values
 Dim l As Long 'counter

 'prep the item
 If Left(sItem, 1) = sDelimiter Then
   sItem = Right(sItem, Len(sItem) - 1)
 End If
 If Right(sItem, 1) <> "=" Then
   sItem = sItem & "="
 End If

 v = Split(sList, sDelimiter)
 For l = 0 To UBound(v)
   If Left(v(l), Len(sItem)) = sItem Then
     Ret = Right(v(l), Len(v(l)) - Len(sItem))
     Exit For
   End If

 GetListItem = Ret
 Exit Function
 Select Case Err.Number
   Case Else
     MsgBox "Error: " & Trim(str(Err.Number)) & vbCrLf & _
       "Desc: " & Err.Description & vbCrLf & vbCrLf & _
       "Module: modCmnUtilStrings, Procedure: GetListItem" _
       , vbCritical, "Error!"
 End Select
 Resume Exit_Proc
End Function

Creative Commons License
GetListItem by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 3,812 times.  This page was last modified 08:35, 6 April 2011 by Jack Leach.   Disclaimers