how to create a vba script that would parse out data from tables in Microsoft Access?

I have a table where I have a number field that contains a CONTROL ID.  In this table, we have a custodian and location fields. So the custodian field contains names of people. It can be one or multiple. Location files contains the file paths for all of the data.  So, I need to combine our custodian field with the location field. So for the example, the custodian field can look like this:

Custodian                                                                                              Location
Smith,John;Doe,Jane;Powell,Dan                                            \\Servername\PathforJohnSmith|\\Servername\PathforJaneDoe|\\Servername\PathforDanPowell.

Keep in mind that the Location paths are not a set number of characters, they have vary. Custodian can contain one value or multiple values so I need my vba script to loop until there are no more names to pair up with the location.  So the end result was Smith,John::\\Servername\PathforJohnSmith;Doe,Jane::\\Servername\PathforJaneDoe;Powell,Dan::\\Servername\PathforDanPowell in one field call All Custodians Locations.


To be able to accomplish this I created a series of queries. I used this expression first: Left([Location],InStr([Location],";")-1) to extract the first location into a separate field, then used the Right([Duplicate Locations],Len([Duplicate Locations])-InStr([Duplicate Locations],"|")) that gave me the remaining locations.  This works on the custodian field with the names as well since both fields had delimiters.

I would have to repeat this process over and over again until all of the locations are extracted. This is the same with the custodian field as well. Then I would make up the fields with the location with concatenated.

I would like to automate this more within an Access database so it won't take me as long as it did for future work. Any suggestions would be happily received.
Laurie KingeDiscovery ManagerAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
PatHartmanConnect With a Mentor Commented:
Where does this data come from?  Are you stuck with keeping it this way or can you normalize it?  You need at least one more table so you can keep a single row per custodian.  That assumes that there is only one location per custodian.  If a custodian may have multiple locations, you'll need a second new table.

Look at the Split() function.  You can use the semi-colon to split the custodian names into an array.  Then you can loop through the array and write a record for each custodian after you separate the first name and last names into SEPARATE fields.

Assuming the data in the location field is in the same order as the custodian, you can use the same array index to separate the location string.  If the data might be in a different order, you are SOL unless you can match on custodian name.

Once you have the data properly normalized, you can mush it all together again if you must to export it but for your own purposes, keep it separate.
0
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
place this function in a regular module

Function ParseInfo(strNames, strPath) As String
Dim j, arrNames() As String, arrPath() As String, strResult As String
arrNames = Split(strNames, ";")
arrPath = Split(strPath, "|")

For j = 0 To UBound(arrNames)
    strResult = strResult & ";" & arrNames(j) & "::" & arrPath(j)
Next
ParseInfo = Mid(strResult, 2)
End Function


then create a query likethis

select  Custodian,Location, ParseInfo([Custodian],[Location])
From yourTable
0
 
Laurie KingeDiscovery ManagerAuthor Commented:
Thank you for your input. Both were very helpful.
0
All Courses

From novice to tech pro — start learning today.