Mapping database address fields

Introduction

This item arose from an engagement letter project I was working on, but should be useful whenever you are trying to translate one set of addresses, or indeed any other, fields to another.

A typical situation would be the need to take the addresses from a database that refers to them as follows:

Address1
Address2
Address3
Town
County
Postcode
Country

and import or link them to our database which has a differently structured set of address fields:

Premises
Town
County
Postcode
Country

Obviously, if our original set of fields has no structure, we then just have to guess which fields are mapped to which other fields. The issue we want to deal with here is condensing the three individual address line fields into the one premises field.

Let’s look at this in stages:

Stage one: concatenation

First of all we will look at a section of an ‘Append’ query that will take our 3 original address1-3 fields and join them together, with commas as separators. The append query will then add this combined address field to the ‘Premises’ field in our current database:

Concatenation

Note that we have included a comma followed by a space between each address line, and used the ‘&’ to join all the bits of the address together.

This is all very well, but of course our premises address will look something like:

Concatenation result

This will make it pretty tricky to generate an address in a letter or on a label.

Stage two: carriage return and line feed

What we really need to do is to put each of the individual fields onto a different line. We can do this by replacing the “, ” bits with the characters that will create the required line feed. These are characters 13 and 10 and we need to enter them using the CHR() function as follows:

Chr(13) & Chr(10)

Here is the complete field:

Separate lines

and the result:

Separate lines result

Stage 3: allow for empty fields

We’re not quite there yet. If any of the address fields are blank, we will end up with blank lines in our new premises field. We need to use the Access IIF() function to check each field and only include it if it has something in it.

In fact it’s worse than that because if there is something in address2 but not in address1, we need to include the contents of address2, but not the carriage return/line feed that precedes it. The same issue occurs if there is something in address3, but nothing in either address1 or address2.

We think the following should do the trick – please let us know if you don’t think it will work properly, or if you are able to come up with a more elegant solution:

Premises: [address1] & IIf([address2]<>””,IIf([address1]<>””,Chr(13) & Chr(10)) & [address2]) & IIf([address3]<>””,IIf([address1] & [address2]<>””,Chr(13) & Chr(10)) & [address3])

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s