I came across some code on http://www.mvps.org/access/ which validates the UK postcode. It was created by John Nurick. I've used it successfully in:
Private Sub Text0_AfterUpdate()
If Not rgxValidate(Me.ActiveControl.Text, "(??: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})") Then
MsgBox "I don't like " & Me.ActiveControl.Text & "."
Cancel = True
End If
End Sub
It calls a function and I could post the code but it's very long and I'm not sure how to post it here in the "compact" format. At the end of the code, John gives several constant declarations so the function can also be used to validate a US phone number, Canadian postcode etc. These are given in the format:
<Public Const rgxZIP_UK =......> (for the UK postcode)
I think it would be really helpful if all of these declarations could be made at the same time, so the function can be called when appropriate, but I'm not sure where to place these constants. They can't go after <Private Sub Text0_AfterUpdate(), nor can they go within the function as these locations cause an error.
Can someone tell me where and how these constants should be declared? I've seen several questions here and on other boards about validation of UK postcodes and I'm sure that this will be very useful.
Thanks in advance.
Juan
Private Sub Text0_AfterUpdate()
If Not rgxValidate(Me.ActiveControl.Text, "(??: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})") Then
MsgBox "I don't like " & Me.ActiveControl.Text & "."
Cancel = True
End If
End Sub
It calls a function and I could post the code but it's very long and I'm not sure how to post it here in the "compact" format. At the end of the code, John gives several constant declarations so the function can also be used to validate a US phone number, Canadian postcode etc. These are given in the format:
<Public Const rgxZIP_UK =......> (for the UK postcode)
I think it would be really helpful if all of these declarations could be made at the same time, so the function can be called when appropriate, but I'm not sure where to place these constants. They can't go after <Private Sub Text0_AfterUpdate(), nor can they go within the function as these locations cause an error.
Can someone tell me where and how these constants should be declared? I've seen several questions here and on other boards about validation of UK postcodes and I'm sure that this will be very useful.
Thanks in advance.
Juan