Results 1 to 34 of 34

Thread: postcodes

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    postcodes

    hi, i am validating a postcode using this code, which puts the space in for the user, but what i really wanted to do was check each letter or use regex but i have never used regex before and i dont know how to check each letter.
    the reason i need this is because the user can just enter 1234567 and it is validated fine, any ideas or good tutorials??

    Code:
    Private Sub TextBox9_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox9.Text = Trim(TextBox9.Text)
    
    Dim strPhone As String
    strPhone = Replace(TextBox9.Text, " ", "")
    
    If Len(strPhone) = 6 Or 7 Then
    strPhone = Mid(strPhone, 1, 4) & " " & Mid(strPhone, 5, Len(strPhone) - 4)
    TextBox9.Text = strPhone
    
    Else
    MsgBox TextBox6.Text & " is not a valid postcode. Please enter a valid postcode.", vbOKOnly, "Invalid postcode"
    TextBox9.Text = ""
    TextBox9.SetFocus
    Cancel = True
    
    End If
    
    End Sub
    thanks
    joe

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: postcodes

    If its an app with internet access you can connect to some of the free online zip code databases in order to verify the code.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    nope its just an excel user input form.

  4. #4
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: postcodes

    pain in the bum post codes
    as they could be two letters then a number or one letter

    you could check to see if the first character is numeric
    if it is then ask them to reinput

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    how would i check that the first letter isnt numeric?

  6. #6
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: postcodes

    vb Code:
    1. Sub test()
    2.  
    3. Dim string1 As String
    4.  
    5. string1 = "12345"
    6.  
    7. If IsNumeric(Left(string1, 1)) Then
    8.     MsgBox "Your postcode does not start with a letter"
    9.     Exit sub
    10. End If
    11.  
    12. End Sub

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    Hi

    Here is another thing that you can do... (It is just a suggestion)

    Assuming that you are from UK, What I suggest that you google for a list of postcodes. As per this website there are 2821 postcodes, which you can copy and paste in one of your sheets in excel. I believe the file which this website offers is not working so you can search for any other site. I am sure that there are plenty of them... You can then use this data to compare/validate with the data that the user inputs...

    If you are from any other country, then you can google for that country's postcode and use them...

    I think that this might be the most simplest way to do it... I could be wrong...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    that sounds like a good solution. what code do i use to match the users input to my list of postcodes?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    i cant seem to find a list of all the total postcodes, but what i have found is a table that displays the area code and which city it stands for, e.g. ng stand for nottingham etc.
    is there any way that i could check that the city the user has entered and the postcode he has entered match up using this table.
    as this would give me half the validation and then just check for length to see if the postcode is the right length.

    any ideas or code?

    thanks

  10. #10
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: postcodes

    you'd first have to check the second character of the entered post code
    see if it is numeric, if it is then you check the first character to the post code list
    if the second character isnt numeric, you would then check the postcode using the two characters

    if that makes sense :s

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    i cant seem to find a list of all the total postcodes
    Let me see if i can get you the list of post codes. can you confirm which country you want the post codes for?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    all Uk postcodes please

    thanks

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    Does this help?

    http://www.kubelabs.com/ukpostcodedata.php

    See right at the bottom of that page or click here
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    so... using this big list of postcodes and where they are from, how would i compare it to my user inputted post code and city/town.

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    let me write a short code for you
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    could you explain it then as you go, as i would like to learn how to do it for myself.. you wont always be here to help..

    or if you could give me some things to look up on google i could have a go my self.

  17. #17
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    Hi Joe

    Here is a very basic example to do the check. I have checked it and it works...

    Please edit the code as per your requirements...

    vb Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. Dim srch As String
    4. Dim C As Range
    5. Dim Flag As Boolean
    6.  
    7. 'Flag to check if postcode is correct
    8. Flag = False
    9.  
    10. 'Say you want the user to input the postcode
    11. 'in Cell D2
    12. 'If it is say a "Textbox1" then assign the Textbox1.value
    13. 'to "srch"
    14. srch = Trim(Range("D2").Value)
    15.  
    16. 'As per the CSV File DATA is from A2 to A3293
    17. 'Replace Sheet2 with the name of the sheet
    18. 'Where the Data is stored...
    19. For Each C In Sheets("Sheet2").Range("A2:A3293")
    20.     If Trim(C.Value) = srch Then
    21.         'postcode found
    22.         Flag = True
    23.         Exit For '<===== [EDIT]: I added this line
    24.     End If
    25. Next C
    26.  
    27. If Flag = False Then
    28.     'or what ever message
    29.     MsgBox "Please enter a valid postcode"
    30. Else
    31.     'or what ever message
    32.     MsgBox "It is a correct postcode"
    33. End If
    34.  
    35. End Sub

    Hope this helps...
    Last edited by Siddharth Rout; Mar 28th, 2007 at 03:40 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    would this work if i put it in the before update event of a textbox??

  19. #19
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    I don't see a reason why it shouldn't

    I have commented the code so you understand what the code does... Please edit the above code as per your requirements.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    ok, thankyou i am just trying it know in the before update event,

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    i tried it using this code,

    Code:
    Private Sub TextBox9_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Dim srch As String
    Dim C As Range
    Dim Flag As Boolean
    'Flag to check if postcode is correct
    Flag = False
    'Say you want the user to input the postcode
    'in Cell D2
    'If it is say a "Textbox1" then assign the Textbox1.value
    'to "srch"
    srch = Trim(TextBox9.Value)
    'As per the CSV File DATA is from A2 to A3293
    'Replace Sheet2 with the name of the sheet
    'Where the Data is stored...
    For Each C In Sheets("Sheet3").Range("A2:A3293")
    If Trim(C.Value) = srch Then
    'postcode found
    Flag = True
    End If
    Next C
    If Flag = False Then
    'or what ever message
    MsgBox "Please enter a valid postcode"
    TextBox9.Text = ""
    TextBox9.SetFocus
    Cancel = True
    Else
    'or what ever message
    
    
    End If
    
    End Sub
    but even when i put in a proper postcode e.g. ng14 5bn, my postcode

    i still get a mesage box asking me to input a valid postcode???

  22. #22
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    but even when i put in a proper postcode e.g. ng14 5bn, my postcode
    That is because, you don't have that postcode in that CSV list...

    Try it with any postcode from that list...

    Also while entering the postecode use Uppercase for the time being

    or change this line

    vb Code:
    1. If Trim(C.Value) = srch Then

    to

    vb Code:
    1. If ucase(Trim(C.Value)) = ucase(srch) Then

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    how else could i validate then using just the list of area codes and citys as i need it to accept all postcodes and the list dosnt have all the postcodes in.
    i tried putting in the first postcode on the list and it still didnt accept??

    what i need is for the code to check the first part of the user inputted postcode against the list and the user inputted city aginst the list and if they are on the same row then the postcode will be pretty much right, and then check its length, if this is right aswell it will be accepted.

    the list i am on about can be found here

    any ideas or help is much appreciated

    Joe

  24. #24
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: postcodes

    you could always insert a combo box so that the user selects the prefix for the postcode....
    that way you know its valid....

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    or i could fill in the first part of the postcode for them depending on what city they put in, this could be a lebel so they coudnt chage it... would this work??

    i would still rather allow them to fill in the whole postcode themselves though

  26. #26
    Addicted Member
    Join Date
    Jan 2007
    Location
    England
    Posts
    234

    Re: postcodes

    London wouldnt work though mate
    as it would depend on the area

  27. #27
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: postcodes

    what i need is for the code to check the first part of the user inputted postcode against the list and the user inputted city aginst the list and if they are on the same row then the postcode will be pretty much right, and then check its length, if this is right aswell it will be accepted.
    Can you mail me the file?

    Let me see what do you exacty have?

    You still have my email address? Correct?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  28. #28

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    ok, sent it.

    thanks

  29. #29
    Hyperactive Member Davadvice's Avatar
    Join Date
    Apr 2007
    Location
    Glasgow (Scotland)
    Posts
    440

    Re: postcodes

    Hello,

    No need for a list of post Codes all though It would be ideal although imposible to keep updated.

    here is a Sub that will validate the UK post code for you.

    Code:
    Function IsPostcode(Postcode As String) As Boolean
    
    Dim sPostcodeLeft As String
    Dim sPostcodeRight As String
    
    Dim Office As String 'First one or two Letters
    Dim District As String 'One or two numbers, or one number and one letter
    Dim Sector As String 'Usually one number (expect variations)
    Dim UnitCode As String 'Always two letters
    
    IsPostcode = False
    
    If Len(Postcode) < 5 Then
        Exit Function
    End If
    
    Postcode = UCase(Postcode)
    
    On Error GoTo EndFunction
    
    If Len(Postcode) <> 8 Then
        sPostcodeRight = Trim(Right(Postcode, 3))
        sPostcodeLeft = Trim(Left(Postcode, Len(Postcode) - 3))
        Do Until Len(sPostcodeLeft) > 4
            sPostcodeLeft = sPostcodeLeft & " "
        Loop
        Postcode = UCase(sPostcodeLeft & sPostcodeRight)
    End If
    
    sPostcodeRight = Trim(Right(Postcode, 3))
    sPostcodeLeft = Trim(Left(Postcode, Len(Postcode) - 3))
    
    If Len(sPostcodeRight) = 3 Then
        Sector = Left(sPostcodeRight, 1)
        UnitCode = Right(sPostcodeRight, 2)
        If Not (Asc(Mid(UnitCode, 1, 1)) >= 65 And Asc(Mid(UnitCode, 1, 1)) <= 90) Then
            Exit Function
        End If
        If Not (Asc(Mid(UnitCode, 2, 1)) >= 65 And Asc(Mid(UnitCode, 2, 1)) <= 90) Then
            Exit Function
        End If
    Else
        Exit Function
    End If
    
    If Len(sPostcodeLeft) >= 2 And Len(sPostcodeLeft) <= 4 Then
        If Asc(Left(sPostcodeLeft, 1)) >= 65 And Asc(Left(sPostcodeLeft, 1)) <= 90 Then
            If (Asc(Mid(sPostcodeLeft, 2, 1)) >= 65 And Asc(Mid(sPostcodeLeft, 2, 1)) <= 90) Then
                Office = Left(sPostcodeLeft, 2)
            ElseIf (Asc(Mid(sPostcodeLeft, 2, 1)) >= 48 And Asc(Mid(sPostcodeLeft, 2, 1)) <= 57) Then
                Office = Left(sPostcodeLeft, 1)
            Else
                Exit Function
            End If
            District = Right(sPostcodeLeft, Len(sPostcodeLeft) - Len(Office))
            If Not (Asc(Left(District, 1)) >= 48 And Asc(Left(District, 1)) <= 57) Then
                Exit Function
            Else
                If Len(District) = 2 Then
                    If Not (Asc(Mid(District, 2, 1)) >= 48 And Asc(Mid(District, 2, 1)) <= 57) And Not (Asc(Mid(District, 2, 1)) >= 65 And Asc(Mid(District, 2, 1)) <= 90) Then
                        Exit Function
                    End If
                ElseIf Len(District) = 1 Then
                    If Not (Asc(District) >= 48 And Asc(District) <= 57) Then
                        Exit Function
                    End If
                Else
                    Exit Function
                End If
            End If
        Else
            Exit Function
        End If
    Else
        Exit Function
    End If
    
    IsPostcode = True
    
    EndFunction:
    End Function
    thanks
    Dav

  30. #30

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    i just seem to get a load of errors when i use this, what code would i put in the before update event of my textbox to call the function?? as this is probs what i am doing wrong.

    thanks joe

  31. #31

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    any ideas anyone, thanks alot Joe

  32. #32
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: postcodes

    OK, to validate it from the list in post 14, all you need to do is split off the last 3 characters, because all UK postcode end in 3 characters, then check what's left against the list. If you want to validate the last 3 as one number, two letters, then that is a very straightforward addition.

    For now:

    VB Code:
    1. Dim c As Range
    2.  
    3. Dim MyPostCode As String
    4. Dim PostCodeStart As String
    5. Dim PostCodeEnd As String
    6.  
    7.  
    8. MyPostCode = UCase(Range("C8").Value)   'Make it upper case
    9.  
    10. 'UK postcode always ends in 3 characters. Beginning part is variable, and is whatever is left.
    11. PostCodeStart = Left$(MyPostCode, Len(MyPostCode) - 3)
    12. PostCodeEnd = Right$(MyPostCode, 3)
    13.  
    14.  
    15. 'Find it in the list on sheet 2
    16.  
    17. Set c = Worksheets("Sheet2").Range("A:A").Find(PostCodeStart, LookIn:=xlValues, LookAt:=xlWhole)
    18.  
    19. If Not (c Is Nothing) Then      'Found it
    20.    
    21.     MsgBox "Postcode: " & PostCodeStart & " " & PostCodeEnd & Chr(13) & "Location: " & c.Offset(0, 1)
    22.  
    23. Else        'Not found
    24.  
    25.     GoTo InvalidPostCode:
    26.  
    27. End If
    28.  
    29.  
    30. Exit Sub
    31.  
    32. 'Error message
    33.  
    34. InvalidPostCode:
    35.  
    36. MsgBox "Please enter a valid postcode"




    Put this in a commandbutton on the form, put a postcode in cell C8 and copy the list of postcodes into columns A to D on sheet 2.


    Then all you need to do is change the input from Range("C8") to to whatever, and off you go.


    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  33. #33

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    110

    Re: postcodes

    the post code is being entered into a textbox on a form though, not a cell. And i would it to check the postcode before the user can leave the focus of the textbox as this is how all my other validation works. any ideas?

  34. #34
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: postcodes

    But that's wholly incidental. Instead of taking your input from the cell, take it from textbox1.text or whatever. And set it to trigger on the Exit event of the textbox.

    Come on, you might have to do a little bit of adapting to your specific needs here.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

Posting Permissions

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



Click Here to Expand Forum to Full Width