Results 1 to 11 of 11
  1. #1
    krpurcel is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2022
    Posts
    3

    Update table using code problem

    Hi,



    Trying to update one field in all records in a table using code attached to a button. Real basic stuff. The code isn't working. The code runs, but I'm adding a new record instead of updating the existing records

    It's been several years since I did any of this. How do I update ALL the records instead of adding one new one?

    Here's the code.

    Code:
    Dim v_team_selected As String
    v_team_selected = Combo26
    
    
    MsgBox (v_team_selected)
    
    
    Dim db As Database
    Set db = CurrentDb
    Dim rs As Recordset
    Set rs = db.OpenRecordset("Game_Records")
    
    
    rs.AddNew
        rs!team_selected = v_team_selected
        MsgBox ("Inside the AddNew. Variable = " + v_team_selected "Next step is update.")
        rs.Update
        MsgBox ("Update step just finished")
    rs.Close
        MsgBox ("rs closed")
    Set rs = Nothing
    MsgBox ("finish!")
    Thanks for looking and any advice!

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    rs.edit instead of rs.AddNew

    Although you can do it with an update query instead of a rs.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    I would have hoped that
    Code:
    rs.AddNew
    would have given it away?

    You might also want to use Debug.Print for output, especially if you have more than a few records to update. Clickling MSGBOX twice per record, is going to get old very quickly.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    krpurcel is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2022
    Posts
    3
    Welshgasman,

    Yes, it was a definite clue. But I could not remember EDIT. And am evidently more challenged than I realized since I wasn't able to google it up. Thanks for the help!

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    Quote Originally Posted by krpurcel View Post
    Welshgasman,

    Yes, it was a definite clue. But I could not remember EDIT. And am evidently more challenged than I realized since I wasn't able to google it up. Thanks for the help!
    That's where intellisense comes in handy.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    krpurcel is offline Novice
    Windows 11 Office 365
    Join Date
    Oct 2022
    Posts
    3
    And intellisense wasn't turned on. Forgot all about that. Thanks moke123!

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    Quote Originally Posted by krpurcel View Post
    And intellisense wasn't turned on. Forgot all about that. Thanks moke123!
    I did not even know you could turn it off?

    So you have one over on me.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    Quote Originally Posted by Welshgasman View Post
    I did not even know you could turn it off?

    So you have one over on me.
    Can You? I always thought it was an early/Late binding thing.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    I suspect that if you can, it is determined by the Compile on Demand option, which I take to mean that when off, a procedure isn't compiled until it is called. Or does it refer to the whole module, regardless if form/report, standard or user defined class? Not explained very well here

    https://learn.microsoft.com/en-us/of...ons-dialog-box
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,656
    Found it, I think.
    In the VBE options- Autolist Members
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,818
    Crap! I had another thread in my noggin. It was about compiling, not intellisense.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Update of Table Problem
    By Erictsang in forum Access
    Replies: 3
    Last Post: 09-19-2017, 07:47 AM
  2. Update table problem
    By mmiklauz in forum Access
    Replies: 9
    Last Post: 07-26-2017, 07:09 AM
  3. problem reading table from VB code
    By nello87to in forum Modules
    Replies: 3
    Last Post: 08-16-2016, 01:59 PM
  4. update query code problem
    By Jen0dorf in forum Access
    Replies: 2
    Last Post: 12-22-2015, 04:35 AM
  5. Update to a table with code
    By jlk in forum Programming
    Replies: 4
    Last Post: 01-07-2013, 03:37 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