Solved

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

Posted on 2014-12-08
3
200 Views
Last Modified: 2016-02-10
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.
0
Comment
Question by:lauriecking
3 Comments
 
LVL 34

Accepted Solution

by:
PatHartman earned 250 total points
Comment Utility
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
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
Comment Utility
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
 

Author Closing Comment

by:lauriecking
Comment Utility
Thank you for your input. Both were very helpful.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now