Access – VBA – Complex String Parsing / Parsing OpenArgs and Tags

It can be very helpful at times to be able to use a form/report’s OpenArgs or a control’s Tag property to pass along settings, say a control’s visibility.

So you could set a control’s tag = “True” and then apply this “setting” by simply doing

Me.ControlName.Visible = Me.Tag

In a more complex example tag = “visible=True” and then your code would like

If (InStr(Me.Tag, "visible") > 0 Then Me.ControlName.Visible = Replace(Me.Tag, "visible=", "")

What About Handling Multiple Settings?

That why I’m writing today. I’ve seen the question numerous times and it all just comes down to 2 things:

  • Following an input standard when create the tag/openargs/…
  • Creating a simple parsing function

Both are illustrated in my function below

'---------------------------------------------------------------------------------------
' Procedure : PasreStrComp
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Parses and returns a specific element value
'               Returns "" if element has no value or isn't found
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' sStrComps     : The String to be parsed (OpenArgs, Tag, ...)
' sCompName     : The Element name you want to return the value of
' sCompDelim    : Seperator used to split the Element name from its value
'                        - optional default is :
' sCompsDelim   : Separator used to segregate element from one another
'                        - optional default is |
'
' Usage:
' ~~~~~~
' PasreStrComp("ContactID:127", "ContactId") -> 127
' PasreStrComp("ContactID:127|Category:Allergies", "Category") -> Allergies
' PasreStrComp("ContactID:127|Category:Allergies", "Bold") -> ""
' PasreStrComp("ContactID:127~Category:Allergies", "Category", , "~") -> Allergies
' PasreStrComp("ContactID|127~Category|Allergies", "Category", "|", "~") -> Allergies
' PasreStrComp("visible=True;bold=False", "bold", "=" , ";") -> False
' PasreStrComp("visible=True;bold", "bold", , ";") -> ""
' PasreStrComp("visible=True;bold=False", "italic", , ";") -> ""
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2011-10-03              Initial Release
' 2         2019-01-19              Public Release
'                                   Updated Copyright
'                                   Added a few more usage examples
' 3         2019-01-20              Switch from Instr to Mid when search for the sCompName
'                                       as InStr could lead to mismatches in certain cases
'---------------------------------------------------------------------------------------
Public Function PasreStrComp(sStrComps As String, _
                             sCompName As String, _
                             Optional sCompDelim As String = ":", _
                             Optional sCompsDelim As String = "|") As String
    On Error GoTo Error_Handler
    Dim aStrComps()           As String
    Dim i                     As Long

    aStrComps() = Split(sStrComps, sCompsDelim)
    For i = 0 To UBound(aStrComps())
        If InStr(aStrComps(i), sCompDelim) > 0 Then
            If Mid(aStrComps(i), 1, InStr(aStrComps(i), sCompDelim) - 1) = sCompName Then
                PasreStrComp = Mid(aStrComps(i), InStr(aStrComps(i), sCompDelim) + 1)
            End If
        End If
    Next i

Error_Handler_Exit:
    On Error Resume Next
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: PasreStrComp" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

So now you can pass along as many settings, properties, values, … whatever you want and easily extract the single one you seek with one straightforward function. So although I created this for dealing with Access tags and openargs, it truly is a versatile function that can be used in any VBA environment to extract single element values from complex strings.

2 responses on “Access – VBA – Complex String Parsing / Parsing OpenArgs and Tags

  1. Maciej

    I’ve came across the same problem recently. The way I worked it out, I create a dictionary object I want to pass, convert it to JSON string, pass it via open args and convert it back to dictionary. It’s is a very convenient solution, and there’s an all-VBA JSON parser that handles the conversion from/ to dictionary:
    https://github.com/VBA-tools/VBA-JSON

    1. Daniel Pineault Post author

      Yes, I’ve seen that project before. It is an interesting approach to OpenArgs, much more attune to what we do in standard web development.

      My only reservation is the overhead of such an approach when compared to this simple parser (13 lines vs. 1123 + all the APIs used). That said, with a JSON approach you can do much more. I guess, like almost anything, it depends on the need.