Results 1 to 14 of 14
  1. #1
    New Lounger
    Join Date
    Feb 2001
    Location
    Southern U.K.
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    UK Postcode Format Check

    Hello

    I need to validate, in VBA, whether or not a string matches the format pattern of a UK postcode - not whether the postcode exists or not, just whether it has a valid format.

    Does anyone have a snippet of VBA code which will do this that they could share with me?

    Thanks

    Paul <img src=/S/grovel.gif border=0 alt=grovel width=31 height=23>

  2. #2
    JustCallMeAl
    Guest

    Re: UK Postcode Format Check

    What's the format? Maybe someone can whip one up....

  3. #3
    New Lounger
    Join Date
    Feb 2001
    Location
    Southern U.K.
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: UK Postcode Format Check

    I am trying to find this out too (I have contacted the Royal Mail and am waiting for a reply). Thought someone else might have already been down this path before........

    Paul <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: UK Postcode Format Check

    When I was working with international mailings, I gave up on even trying to validate a postal code pattern. I just upper cased everything and left it at that. If the mail service ever gives you a consistent pattern, post it and someone can help you with the code if need be.
    Charlotte

  5. #5
    New Lounger
    Join Date
    Feb 2001
    Location
    Southern U.K.
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: UK Postcode Format Check

    Thanks Charlotte, will do.

    Paul <img src=/S/sigh.gif border=0 alt=sigh width=15 height=15>

  6. #6
    New Lounger
    Join Date
    Feb 2001
    Location
    Southern U.K.
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: UK Postcode Format Check

    Hi, I found this on the net for the UK postcode format:

    L(L)(N)(L) NLL

    where

    L = Letter
    N = Number
    () may or may not be there in correct postcodes

    I think ignoring/stripping the space would be a good start, but I don't see that the like operator is flexible enough to handle this anyway?

    Any ideas?

    Paul <img src=/S/help.gif border=0 alt=help width=23 height=15>

  7. #7
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: UK Postcode Format Check

    I think that you should leave the space in. The bit before the space is know as an region. As an example, take my work post code KT17 4RS

    The KT represents an district - Kingston Themes.
    The KT17 represents a region.
    The KT17 4 represents an area.

    Clear? No not very but that is the british way for you.

    Cheers,

    Tim
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  8. #8
    Francis Winter
    Guest

    Re: UK Postcode Format Check

    Remember that N here may be a number with more than one digit. It seems, however, that the format is even a little more complex than this quite captures.

    See for discussion and an Access approach <A target="_blank" HREF=http://www.microsoft.com/uk/office/column16.htm>http://www.microsoft.com/uk/office/column16.htm</A>

  9. #9
    Francis Winter
    Guest

    Re: UK Postcode Format Check

    A rough VBA draft based on the discussion at the URL quoted in the other post.

    <pre>Public Function IsPostCode(Chars As String) As Boolean
    Const DIGIT As String = "#"
    Const LETTER As String = "[A-Za-z]"
    Const DIGIT_OR_LETTER As String = "[A-Za-z0-9]"
    Const SPACE_DIGIT_OR_LETTER As String = "[ A-Za-z0-9]"

    'To insist on upper case letters (technically correct) use these
    'alternative constant declarations
    ' Const LETTER As String = "[A-Z]"
    ' Const DIGIT_OR_LETTER As String = "[A-Z0-9]"
    ' Const SPACE_DIGIT_OR_LETTER As String = "[ A-Z0-9]"

    Dim astrChars() As String
    Dim strChars As String
    Dim lngChars As Long
    Dim iChar As Long
    'Only strings of 8 or less chars are valid
    strChars = Trim$(Chars)
    lngChars = Len(strChars)
    If lngChars > 8 Then Exit Function
    'get the characters into an array for simplicity
    ReDim astrChars(1 To lngChars)
    For iChar = 1 To lngChars
    astrChars(iChar) = Mid$(strChars, iChar, 1)
    Next
    'Check characters one by one from left to right
    If astrChars(1) Like LETTER Then
    If astrChars(2) Like DIGIT_OR_LETTER Then
    If astrChars(3) Like SPACE_DIGIT_OR_LETTER Then
    If astrChars(4) Like SPACE_DIGIT_OR_LETTER Then
    If astrChars(5) Like SPACE_DIGIT_OR_LETTER Then
    If astrChars(6) Like DIGIT_OR_LETTER Then
    Select Case lngChars
    Case 6
    IsPostCode = True
    Case 7
    IsPostCode = (astrChars(7) Like LETTER)
    Case 8
    If astrChars(7) Like LETTER Then _
    IsPostCode = (astrChars(8) Like LETTER)
    End Select
    End If
    End If
    End If
    End If
    End If
    End If

    End Function</pre>


  10. #10
    Francis Winter
    Guest

    Re: UK Postcode Format Check

    A line went missing from the above. To handle short strings correctly, <pre>If lngChars > 8 Then Exit Function</pre>

    should, of course, be followed immediately by <pre> If lngChars < 6 Then Exit Function</pre>


  11. #11
    Jim_WW
    Guest

    Re: UK Postcode Format Check

    In Rimmers breakdown of the postcode, some of the London postcodes also allow the last "L" to be "N", eg SW1

    Jim_WW

  12. #12
    Xiao Bin
    Guest

    Re: UK Postcode Format Check

    Or something like this, perhaps ...

    <pre>Function IsPostCode(Chars As String) As Boolean
    'two letters at start of string
    Const AREA As String = "^[A-Z]{1,2}"
    'a digit, optionally followed by a digit or letter
    Const DISTRICT As String = "[0-9][A-Z0-9]{0,1}"
    'a single digit
    Const SECTOR As String = "[0-9]"
    'two letters,excluding C,I,K,M,O,V, at end of string
    Const WALK As String = "[A-BD-HJLNP-UW-Z]{2}$"

    'requires vbscript.dll/2 (part of Vbscript download from MS)
    'and a project reference to Microsoft VBScript Regular Expressions
    Dim oRegex As RegExp

    Set oRegex = New RegExp
    With oRegex
    .Pattern = _
    AREA & DISTRICT & " " & SECTOR & WALK

    .IgnoreCase = False
    IsPostCode = .Test(Chars)
    End With
    End Function</pre>


  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: UK Postcode Format Check

    Start with

    <A target="_blank" HREF=http://bitboost.com/ref/international-address-formats.html>http://bitboost.com/ref/internationa...s-formats.html</A>

  14. #14
    New Lounger
    Join Date
    Feb 2001
    Location
    Southern U.K.
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: UK Postcode Format Check

    Just come back after a long weekend.

    Thanks for all your help everyone!

    Paul <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •