Access Table Input Masks

W

WilliamE

I have a database table with one of the fields being postcode. Access
originally sets this up as the US Postal Code I can change it to the UK
Postcode Format However since in the UK The following formats are used viz.
LL0 0LL OR L00 0LL I find it impossible to get both of these to work in the
same field . I beleive there must be a way but just cant think how to do it.
any idea of the way to do this would be greatfully received
 
J

John Vinson

did not work for me with postcode MK40 1NG

Nope. Need to persuade Her Majesty's Post to come into the 21st
century. Good luck....

AFAIK there's no way to do this with an input mask, they're just not
flexible enough; you need to use VBA code in the textbox's
BeforeUpdate event to validate the format.

John W. Vinson[MVP]
 
T

Tim Ferguson

AFAIK there's no way to do this with an input mask, they're just not
flexible enough; you need to use VBA code in the textbox's
BeforeUpdate event to validate the format.

I generally don't bother, but this is the reference that I keep in my
archive:

http://www.access-programmers.co.uk/forums/showthread.php?t=97666


'Official formatting of postcodes and the like may change
'over time. Some of these expressions may need adjustment
' to bring them up to date.
'UK Postcode
Public Const rgxZIP_UK = "(?:(?:A[BL]|B[ABDHLNRST]?|" _
& "C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|" _
& "H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|" _
& "N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|" _
& "T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)" _
& "\d(?:\d|[A-Z])? \d[A-Z]{2})"
'A simpler expression that does not check for valid postcode areas:
' "(?:[A-Z]{1,2}\d(?:\d|[A-Z])? \d[A-Z]{2})"


There is similar code here: ->
http://www.mvps.org/access/modules/mdl0063.htm


There is more information at

http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm
http://www.evoxfacilities.co.uk/evoxps.htm

Hope that helps


Tim F
 
J

Jamie Collins

you need to use VBA code in the textbox's
BeforeUpdate event tovalidatethe format.I generally don't bother, but this is the reference that I keep in my
archive:

http://www.access-programmers.co.uk/forums/showthread.php?t=97666

'Official formatting of postcodes and the like may change
'over time. Some of these expressions may need adjustment
' to bring them up to date.
'UK Postcode
Public Const rgxZIP_UK = "(?:(?:A[BL]|B[ABDHLNRST]?|" _
& "C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|" _
& "H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|" _
& "N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|" _
& "T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)" _
& "\d(?:\d|[A-Z])? \d[A-Z]{2})"
'A simpler expression that does not check for valid postcode areas:
' "(?:[A-Z]{1,2}\d(?:\d|[A-Z])? \d[A-Z]{2})"

A few ideas: constraints are best in the database; multiple simple
rules are better than one complex rule e.g. improved granularity of
error message:

[ANSI-92 query mode wildcard characters]

CREATE TABLE UKPostalAddresses (
postcode VARCHAR(8),
CONSTRAINT uk_postcode__pattern
CHECK
(
postcode LIKE '[A-Z][0-9] [0-9][A-Z][A-Z]'
OR postcode LIKE '[A-Z][0-9][0-9] [0-9][A-Z][A-Z]'
OR postcode LIKE '[A-Z][A-Z][0-9] [0-9][A-Z][A-Z]'
OR postcode LIKE '[A-Z][A-Z][0-9][0-9] [0-9][A-Z][A-Z]'
OR postcode LIKE '[A-Z][0-9][A-Z] [0-9][A-Z][A-Z]'
OR postcode LIKE '[A-Z][A-Z][0-9][A-Z] [0-9][A-Z][A-Z]'
),


CONSTRAINT uk_postcode__invalid_chars_pos_1
CHECK (postcode NOT LIKE '[QVX]%'),


CONSTRAINT uk_postcode__invalid_chars_pos_2
CHECK (postcode NOT LIKE '_[IJZ]%'),


CONSTRAINT uk_postcode__valid_chars_pos_3
CHECK
(
1 = IIF(postcode LIKE '[A-Z][0-9][A-Z]%',
IIF(postcode LIKE '[A-Z][0-9][ABCDEFGHJKSTUW]%', 1, 0), 1)
),


CONSTRAINT uk_postcode__valid_chars_pos_4
CHECK
(
1 = IIF(postcode LIKE '[A-Z][A-Z][0-9][A-Z]%',
IIF(postcode LIKE '[A-Z][A-Z][0-9][ABEHMNPRVWXY]%', 1, 0), 1)
),


CONSTRAINT uk_postcode__valid_chars_inward_part
CHECK
(
postcode NOT LIKE '%[CIKMOV]_'
AND postcode NOT LIKE '%[CIKMOV]'
)
);

Jamie.

--
 
T

Tim Ferguson

A few ideas: constraints are best in the database; multiple simple
rules are better than one complex rule e.g. improved granularity of
error message:

Trouble is (a) not all postcodes that fit these patterns are valid; and (b)
there is every likelihood that new valid postcodes will be created in the
future.

My own view is that you either do the thing properly, i.e. validate the
address itself against the current PAF (post office address file), which
requires a subscription, or not at all. YMMV.


All the best


Tim
 
J

Jamie Collins

Tim said:
Trouble is (a) not all postcodes that fit these patterns are valid...

My own view is that you either do the thing properly, i.e. validate the
address itself against the current PAF (post office address file), which
requires a subscription, or not at all. YMMV.

You are confusing validation and verification, two distinct
requirements.

Verification, a process of determining whether an entity exists in the
reality being modelled, is external to the database _by definition_.
Think of a web service that verifies that a postcode actually exists:
do you think it would be good idea to hard-wire it into the SQL DDL
design, even if it were possible (as it is with CLR constraints in SQL
Server)?

The fact you cannot in the data model verify that a postcode exists is
no excuse for not validating its basic pattern in the database. If a
data value does not fit the basic pattern (i.e. is not a legal value)
then you *know* it cannot exist, which makes it a worthwhile thing to
do.
(b)
there is every likelihood that new valid postcodes will be created in the
future.

Again, the fact that business rules may change in the future (e.g. say
the basic pattern is altered by the Royal Mail to accommodate a greater
range of values) is no argument for avoiding implementation the
business rules in their current guise today. If you think the rules are
likely to change in the lifetime of the app (personally, I suspect the
postcode patterns will remain static for many years to come) then put
the patterns in a table and use table-level CHECK constraints with the
LIKE predicate to validate values.

Jamie.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top