Postcode Anywhere Address Management Web Service
HOME PRODUCTS DEVELOPERS RESELLERS CONTACT US MY ACCOUNT
Developer home
Getting started
Tutorials
Code samples
Documentation and reference
Downloads
Service information
Call us: 0800 047 0493
Home - Developer Centre - Tutorials - ASP quick start

ASP quickstart

Using Postcode Anywhere in your ASP web site is easier than you might think. There's no need to install any special components on the server and you don't need any special skills either.

To access the service, we use some little known extensions built into ADO for accessing remote data sources. This means that you get back a familiar recordset which you can scroll through, filter and sort just like it's from a local server!

Preparation

Before starting the tutorial, we need to create 3 ASP pages: 1 for the postcode text box and find button; 1 for the list of properties in the postcode and the final page showing the address. (Note: These 3 pages can be grouped together at a later stage if you wish to do so.)

page1.asp: Page to enter postcode
<form method=post action="page2.asp">
	Enter postcode
	<input type=text name=postcode><input type=submit value="Find">
</form>
page2.asp: Page listing properties in the postcode
<form method=post action="page3.asp">
	Select your address
	<select name=addressid size=10>
	</select>
	<br>
	<br>
	<input type=submit value="Select">
</form>
page3.asp: Page showing the address
<table>
	<tr>
		<td>Company name</td>
		<td></td>
	</tr>
	<tr>
		<td>Line 1</td>
		<td></td>
	</tr>
	<tr>
		<td>Line 2</td>
		<td></td>
	</tr>
	<tr>
		<td>Line 3/td>
		<td></td>
	</tr>
	<tr>
		<td>Town</td>
		<td></td>
	</tr>
	<tr>
		<td>County</td>
		<td></td>
	</tr>
	<tr>
		<td>Postcode</td>
		<td></td>
	</tr>
</table>

Now you're ready to start making this work with Postcode Anywhere!

In page2.asp we need to populate the select list with the properties in the postcode entered in page1.asp. Then in page3.asp we need to get the fully formatted address which was selected in page2.asp.

Step 1: Finding the properties in a postcode

Getting the addresses in a postcode is just a matter of building a URL containing the request, passing this to the Postcode Anywhere servers and reading the response back into a recordset.

If you're working with a regular VB application, you can create your recordset and just supply the URL instead of the SQL statement. BUT this won't work in IIS. In fact, it will work but will eventually break IIS. The reason is that by default it uses an HTTP implementation which isn't safe for use in a server and will eventually break under any stress.

Instead we need to use a different HTTP stack which is part of the Microsoft XML parser (which is normally preinstalled on IIS). This is much more efficient and does not have any problems when used in IIS. The code is a little more complex but completely reliable.

This function takes a URL and returns a recordset:

Function GetRecordset(URL)

	Dim objHttp
	Dim rst
	
	'Fetch the data
	Set objHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
	objHttp.Open "GET", URL, false
	objHttp.Send
	
	'Load it into the recordset
	Set rst = Server.CreateObject("ADODB.Recordset")
	rst.Open objHttp.ResponseStream
		
	'Return the recordset
	Set GetRecordset = rst

End Function

All we need to do now is build the URL, get the recordset and copy the results into the select list. From the documentation, we know that the ByPostcode method requires the account_code, license_code, action, type and postcode parameters:

strUrl = "http://services.postcodeanywhere.co.uk/recordset.aspx?"
strUrl = strUrl & "account_code=AAAAA11111&"
strUrl = strUrl & "license_code=AA11-AA11-AA11-AA11&"
strUrl = strUrl & "action=lookup&"
strUrl = strUrl & "postcode=" & Request("postcode")

Making the request is now quite straightforward. Once we have the results we need to test for an error as well. A server error is returned as a recordset with 2 fields (number and description). We can test for an error and display it to the user:

Set rst = GetRecordset(strUrl)
If rst.Fields.Count=2 Then
	Reponse.Write "Error: " & rst.Fields(1)
	Response.End
End If

Finally we just work through the recordset and copy the items into the select list:

<select name=addressid size=10>
<%
While Not rst.EOF
	%>
		<option value="<% =rst.Fields("id") %>">
		<% =rst.Fields("description") %>
		</option>
	<%	
	rst.MoveNext
Wend
%>
</select>

That's it! When you enter a postcode in page1.asp and click on the Find button you will have a list of the properties in that postcode. Now we need to get the address when it's selected.

Step 2: Fetching the full address

Once the user selects a property, we need to get the fully formatted address. We're only interested in the address here but you can also get information like grid references if required.

Fetching the address is a very similar process to getting the list before. We are using a different method this time called FetchAddress. The documentation tells us that the method requires account_code, license_code, action and id parameters. Therefore it's just a case of building a slightly different URL to what we used earlier:

strUrl = "http://services.postcodeanywhere.co.uk/recordset.aspx?"
strUrl = strUrl & "account_code=AAAAA11111&"
strUrl = strUrl & "license_code=AA11-AA11-AA11-AA11&"
strUrl = strUrl & "action=fetch&"
strUrl = strUrl & "id=" & Request("addressid")

Once again we need to perform some error checking and finally write out the results into table:

<table>
	<tr>
		<td>Company name</td>
		<td><% = rst.Fields("organisation_name") %></td>
	</tr>
	<tr>
		<td>Line 1</td>
		<td><% = rst.Fields("line1") %></td>
	</tr>
	<tr>
		<td>Line 2</td>
		<td><% = rst.Fields("line2") %></td>
	</tr>
	<tr>
		<td>Line 3/td>
		<td><% = rst.Fields("line3") %></td>
	</tr>
	<tr>
		<td>Town</td>
		<td><% = rst.Fields("post_town") %></td>
	</tr>
	<tr>
		<td>County</td>
		<td><% = rst.Fields("county") %></td>
	</tr>
	<tr>
		<td>Postcode</td>
		<td><% = rst.Fields("postcode") %></td>
	</tr>
</table>

All done!

You should now have your very own ASP address finding application. Don't forget you can download our version of the code from the top of this page if you've experienced any problems.

Try it yourself

Why not try this out yourself and see how you get on? You can click on the icon below to download the pages and get them working on your own PC.
Click to download
Note: to use this sample, you will need an account and web service license key. If you don't have either of these, you need to register to get an account then click on "Setup my account" to get a web service license key.