I need city and state to populate after I enter the Zip Code in Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imnotsmart
    New Member
    • Jul 2007
    • 7

    I need city and state to populate after I enter the Zip Code in Access

    I have an Access DB that I'm looking to have the City, State, and some other info auto populate after I type the Zip. I am VERY VERY new to Access and am teaching myself. I don't know VB and would need to be walked through step by step. Any help would be possible.

    Thanks
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, imnotsmart. Welcome to TSDN!

    I'm going to go ahead and move this thread to the Access forum, where our resident Experts will be better able to help you out.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      Is it safe to assume you have a table listing all cities, states, and zip code?

      Comment

      • imnotsmart
        New Member
        • Jul 2007
        • 7

        #4
        Originally posted by Rabbit
        Is it safe to assume you have a table listing all cities, states, and zip code?
        Yes. I have the table with all the Towns, Cities, Zips and other info I need.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          In the after update event of the zip code control you'd have something along the lines of:

          [Code=vb]
          Private Sub txtZip_AfterUpd ate()
          txtCity = DLookup("City", "tbl_ZipCitySta te", "Zip = " & Me.txtZip)
          txtState = DLookup("State" , "tbl_ZipCitySta te", "Zip = " & Me.txtZip)
          End Sub
          [/Code]
          This assumes zip is a numeric data type.

          Comment

          • imnotsmart
            New Member
            • Jul 2007
            • 7

            #6
            The Zip is a text data type (Zip codes in NJ start with 0. If its numeric the 0 gets dropped). The table is called City. The fields are City, State, Zip. So based on what you posted before, I tried this:

            Private Sub Zip_AfterUpdate ()
            txtCity = DLookup("City", "tbl_City", "Zip = " & Me.txtZip)
            txtState = DLookup("State" , "tbl_City", "Zip = " & Me.txtZip)
            End Sub

            When I go back to the form, I get the following error.

            Compile error:
            Method or data member not found

            .txtZip on line 2 is highlighted in blue and the first line is highlighted in yellow.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              If Zip's text then you'll need to do:
              [Code=vb]
              "Zip = '" & Me.txtZip & "'"
              [/Code]

              txtZip, txtCity, and txtState are in reference to the controls on your form, I doubt that's what you named them so you'll have to change those to match.

              Comment

              • imnotsmart
                New Member
                • Jul 2007
                • 7

                #8
                Thanks!! I played with it a little and got it to work.

                Now, another question. I have yes/no buttons associated with each town and a number. Is the code the same or does it change.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  Originally posted by imnotsmart
                  Thanks!! I played with it a little and got it to work.

                  Now, another question. I have yes/no buttons associated with each town and a number. Is the code the same or does it change.
                  I'm not following, can you go into more detail?

                  Comment

                  • imnotsmart
                    New Member
                    • Jul 2007
                    • 7

                    #10
                    Nevermind, I figured it out.

                    But I have another question. I need the info that populates in the City, State and Zip field to fill in City, State, and Zip fields in another table. Don't know if this is possible.

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      Originally posted by imnotsmart
                      Nevermind, I figured it out.

                      But I have another question. I need the info that populates in the City, State and Zip field to fill in City, State, and Zip fields in another table. Don't know if this is possible.
                      You mean into an additional table? You'll need to use DoCmd.RunSQL "INSERT INTO ..."

                      Comment

                      • imnotsmart
                        New Member
                        • Jul 2007
                        • 7

                        #12
                        Yes, into an additional table called People.

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          Ok, then try out what I suggested in post #11.

                          Comment

                          • imnotsmart
                            New Member
                            • Jul 2007
                            • 7

                            #14
                            Not sure how to do it. Can you give a little more. Here is some more info.

                            After the City, State, and Zip are entered for a Person in my form, I need the City, State, and Zip to also show up under that Person in a different table.

                            Comment

                            • Rabbit
                              Recognized Expert MVP
                              • Jan 2007
                              • 12517

                              #15
                              Originally posted by imnotsmart
                              Not sure how to do it. Can you give a little more. Here is some more info.

                              After the City, State, and Zip are entered for a Person in my form, I need the City, State, and Zip to also show up under that Person in a different table.
                              What you'll want to do is, in the after update event of each of the fields, check that all 3 fields are populated, i.e. not null. Then you want to check to see if a record already exists in the other table. If it does, you use DoCmd.RunSQL "UPDATE SQL String". If not, you use a DoCmd.RunSQL "INSERT INTO SQL Append String"

                              Comment

                              Working...