Results 1 to 4 of 4
  1. #1
    Tman is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2010
    Posts
    7

    VBA Code to auto update a field in another table?

    hi access forums

    Is there a VBA code that can automatically update the same fields in other related tables, for example when the user selects a 'shop' in the front page survey, this response auto updates to other tables (just that field)? eg attached as an after update on the home page submit button?

    I have tried the append query, and I don't think that is as user friendly or as professional as a vba code would be (updating quietly in the background) as there is around 10 tables to update?



    any help appreciated!

    thanks
    TB

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    One method:

    Code:
      Dim db                      As DAO.Database
      Dim rs                      As DAO.Recordset
    
      Set db = CurrentDb()
      Set rs = db.OpenRecordset("TableName", dbOpenDynaset)
    
      rs.AddNew
      rs!FieldName = YourValueHere
      rs!AnotherField = SomeOtherValue
      rs.Update
    
      Set rs = Nothing
      Set db = Nothing
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Tman is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2010
    Posts
    7

    VBA Code to auto update a field in another table?

    Quote Originally Posted by pbaldy View Post
    One method:

    Code:
      Dim db                      As DAO.Database
      Dim rs                      As DAO.Recordset
     
      Set db = CurrentDb()
      Set rs = db.OpenRecordset("TableName", dbOpenDynaset)
     
      rs.AddNew
      rs!FieldName = YourValueHere
      rs!AnotherField = SomeOtherValue
      rs.Update
     
      Set rs = Nothing
      Set db = Nothing
    Thanks pbaldy, however I couldn't get to work.

    I copied this into the form for "tbl1" in the after event VBA, and when I went to new on that form with this added in, I got an error re the fields.

    With the fields below, I think this is where i get it wrong.

    I gather I change the FieldName to my FieldName, however the value I was not sure of. Is there some additional code that goes in here? The value will be whatever the value is in tbl1, eg;

    rs!FieldName = YourValueHere

    change to:

    rs!Site_ID (field in table 2) = [tb1] (table of where I want data from)[Site_ID] (field from table 1 where i want the data from)

    Am I reading that right?

    Should there be code where you have "Your value here" (ignore mine above as i dont know what it would be!? The value is whatever the value in Table 1.

    Many thanks!
    TMan

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    From your initial description, I assume that the desired value is on the same form, so

    rs!Site_ID = Me.Site_ID

    or whatever the name of the control is that contains the desired value. If it's actually only in a table, you'd either need another recordset or a DLookup to get it from there.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Auto Populating a field in a table
    By softspoken in forum Access
    Replies: 11
    Last Post: 04-05-2010, 02:52 PM
  2. Auto-fill field not saving to table
    By aaid in forum Forms
    Replies: 1
    Last Post: 12-18-2009, 05:34 PM
  3. Auto Populated Field not Saving to Table
    By EstesExpress in forum Forms
    Replies: 4
    Last Post: 10-09-2009, 03:33 AM
  4. Replies: 4
    Last Post: 09-03-2009, 02:01 PM
  5. Replies: 1
    Last Post: 03-08-2009, 01:50 PM

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