Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 559
  • Last Modified:

Access Expression to parse a delimited string

I have an Access table containing a pipe-delimited string in a single field. The string looks something like this...

Fieldname = fullText
value1|other value2|some other value3|yet another value4|and a final value5|

I need help writing an Access Expression which will take the string above and break it into separate fields like this...

Field       Value
value1    value1
value2    other value2
value3    some other value3
value4    yet another value4
value5    and a final value5

Can you help me with this? (I'm more familiar with expressions than VB but will accept a VB approach if you can walk me through how to implement.)
  • 4
  • 2
  • 2
  • +1
1 Solution
Jeffrey CoachmanCommented:
Will the string always have the same amount of delimiters?

What about "Blanks"?: SomeText||SomeMoreText
Is this string field in the same table that "target" fields are in?
P-DaddyAuthor Commented:
1) Yes, the string will always have the same number of delimiters
2) Yes, there may be blanks between delimiters, like ||
3) Yes, the string field is in the same table as the target fields but could be separated if necessary.
So in your example are you saying you would end up with a table having 5 columns with each delimited string in each field?  If so, couldn't you just import the delimited file straight in to the table?
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Jeffrey CoachmanCommented:
<2) Yes, there may be blanks between delimiters, like ||>
So then what do you want to happen in this case...?

I think IrogSinta has your solution here.
P-DaddyAuthor Commented:
I wish I could, but the string is quite long and the whole string is actually not available for me to import. It is stored broken up into 12 132 character fields in its original table. What I've done so far is to concatenate all this so at least I have the full string in one field. Now I need your help in parsing the string at each pipe. Easy to do in Excel, I'm just not familiar with how to write this expression in Access. Thanks in advance!
P-DaddyAuthor Commented:
To answer the other comment above if there's a blank between pipe separators I would expect a null value to be written in that field. Does that make sense?
Assuming you already have a table you want to store these 5 fields into you could use code similar to this:
Private Sub ParseAString()
    Dim rst As Recordset
    Dim strToParse As String
    Dim arrParsed
    strToParse = DLookup("[FieldName]","TableName")  'this is the table that holds your string.
    arrParsed = Split(strToParse, "|")
    Set rst = CurrentDb.OpenRecordset("NameOfTable")  'this is the table to store the 5 fields.
    For i = 0 To 4
        rst(i).Value = arrParsed(i)
    Set rst = Nothing
End Sub

Open in new window

Did your table have more than one record to parse?
Gustav BrockCIOCommented:
In a query you can have an expression to retrieve each value/item:

value1: GetFullTextItem([FullText], 0)
value2: GetFullTextItem([FullText], 1)
value3: GetFullTextItem([FullText], 2)
value4: GetFullTextItem([FullText], 3)
value5: GetFullTextItem([FullText], 4)

using this function:

Public Function GetFullTextItem( _
  ByVal strFullText As String, _
  ByVal intItem As Integer) As String
  On Error Resume Next
  GetFullTextItem = Split(strFullText, "|")(intItem)

End Function

P-DaddyAuthor Commented:
I awarded this as the solution of choice because it was the most elegant solution of the two with the least amount of code and I could perform both concatenate and parse steps in the same query. However, I said I was a novice and your solution left me having to do additional research on how to create functions and call them from my query and I had to find out how to replace the [FullText] to get this to work. I know that's just my being a newbie, but you could have easily added some additional help to your solution.

Nevertheless, I could not have gotten past this without you. Thank you!
Gustav BrockCIOCommented:
You are welcome!

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now