Results 1 to 4 of 4
  1. #1
    autiger58 is offline Novice
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Posts
    22

    Update a Table Question

    Hi:



    I have a VBA that will add a city and state when you enter a zip code using a form. What I sometimes need to do is import the same data in access (data with a zip code but without the city and state) and then run some kind of code that will look at the zip code field in the first table, look up the zip code in a second table and then return to the first table and add the city and state that matches that zip code in the city and state fields.

    With the VBA I currently have, it does this as I enter the zip on a form. Can anyone point me in the direction of how I can accomplish this same task under the situation I described above?

    Thanks in advance.

    Michael

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    You can't run code at the table level you either need to run code on a form or through a module.

    If you want to run your process without having a form open create a module, add a function to the module then run the module.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Two options:
    1) loop through each record and update (one at a time)
    or
    2) use an update query to update all of the city and state fields for a specific Zip. If you have 5 records with the same Zip, all 5 would be updated at the same time.

    The pseudo code would be like
    One record at a time:
    Code:
    'Open a records set to select the records
    Single record: "SELECT Zip From TABLE1 WHERE Not IsNull(Zip ) and IsNull(City) and IsNull(State) Order By Zip"
    
    If recordset returns records
    
    'Start loop
    'open a second recordset on Table2 to get the city and State
    "SELECT City, State FROM TABLE2 WHERE Zip = '" & RS1("Zip") & "';"
    
    If recordset returns records
    
    rs.Edit
    rs("City") = varCity  ',- variable name
    rs("City") = varState  ',- variable name
    rs.Update
    
    Close second recordset (table 2)
    
    Loop
    
    'do cleanup stuff
    Group records:
    Code:
    'Open a records set to select the records
    Group records: "SELECT DISTINCT Zip From TABLE1 WHERE Not IsNull(Zip ) and IsNull(City) and IsNull(State) Order By Zip"
    
    If recordset returns records
    
    'Start loop
    
    'open a second recordset on Table2 to get the city and State
    "SELECT City, State FROM TABLE2 WHERE Zip = '" & RS1("Zip") & "';"
    
    If recordset returns records
    
    
    CurrentdB.Execute "UPDATE Table1 SET Table1.City = '" & varCity & "', Table1.State = '"& varState &"';"
    
    Close second recordset (table 2)
    
    Loop
    
    'do cleanup stuff
    This can be run from a button on the form.....

  4. #4
    autiger58 is offline Novice
    Windows 8 Access 2013
    Join Date
    Feb 2015
    Posts
    22
    Thanks Steve. I think I understand it now. I really appreciate it.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Auto Update Question
    By LionsCricket in forum Access
    Replies: 3
    Last Post: 04-11-2013, 03:22 PM
  2. Replies: 1
    Last Post: 02-08-2012, 04:50 PM
  3. Replies: 1
    Last Post: 01-22-2011, 12:23 PM
  4. Update Query Question
    By Profector in forum Queries
    Replies: 2
    Last Post: 10-13-2010, 09:24 AM
  5. Update question
    By oakoen in forum Access
    Replies: 3
    Last Post: 08-28-2009, 07:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums