Solved

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

Posted on 2014-12-08
3
204 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 35

Accepted Solution

by:
PatHartman earned 250 total points
ID: 40487800
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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 40487811
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
ID: 40609200
Thank you for your input. Both were very helpful.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

825 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