Results 1 to 11 of 11
  1. #1
    Jerry Call is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Location
    Champaign, Illinois
    Posts
    85

    Can For Each function be used to edit records rather than just extracting information?


    As I understand it, the For Each function works well for navigating through a set of records and recording information. But from what I read it seems that one can't use it to edit information in a particular field.
    What I need is code to loop through every record in a particular table (call it MyTable) and set the value of the (numeric) CumTally field in every record to zero.

    If For Each is not the best method, what is?

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Quote Originally Posted by Jerry Call View Post
    As I understand it, the For Each function works well for navigating through a set of records and recording information. But from what I read it seems that one can't use it to edit information in a particular field.
    What I need is code to loop through every record in a particular table (call it MyTable) and set the value of the (numeric) CumTally field in every record to zero.

    If For Each is not the best method, what is?
    You could use a recordset and loop through every record from beginning to end and change field values as you progress through the recordset but it would be mus quicker a simpler to create an Update query.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    How about SQL? But before you run this make sure that is what you want/need! Update queries, like Delete queries, can be very unforgiving.
    I suggest making a backup of MyTable before doing major update. Once you confirm your result, you can delete the backup.
    Code:
    UPDATE MyTable
    Set CumTally = 0;
    Last edited by orange; 08-25-2020 at 07:43 AM. Reason: spelling

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    If For Each is not the best method, what is?
    I cant think of one instance where I've used a for each loop with a recordset.
    Generally you'd use a do loop and test for .EOF

    As Bob and Orange have suggested an update query is the way to go. Its not only easier, it's faster.

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    I would venture to say that For Each is for looping over collections whose members are usually objects, not records.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Jerry Call is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Location
    Champaign, Illinois
    Posts
    85
    Gee, can it really be that simple? Will let you know how it comes out. Not to worry. The table in question is small, and not yet in production.
    If it crashes I can easily reconstruct it.

  7. #7
    Jerry Call is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Location
    Champaign, Illinois
    Posts
    85
    Well, gang, didn't get too far. Here's my code for a cmdZero button on frmSodaTallyNorthStore:

    UPDATE tblSodaTallyNorthStore
    Set CumTally = 0;

    This produced a red second line, so obviously the syntax must be wrong.

  8. #8
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Try:
    Code:
    CurrentDb.Execute "UPDATE tblSodaTallyNorthStore Set CumTally = 0;"
    in the Click event of your button
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  9. #9
    Jerry Call is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    May 2020
    Location
    Champaign, Illinois
    Posts
    85
    Bob: Your reputation is worth much more than one star in my grateful eyes! That code not only did the trick (with the addition of a requery command),
    but gave me a useful model for the future.

  10. #10
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Quote Originally Posted by Jerry Call View Post
    Bob: Your reputation is worth much more than one star in my grateful eyes! That code not only did the trick (with the addition of a requery command),
    but gave me a useful model for the future.
    Thank you for your kind words. Glad you got it working
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Dont forget the DbFailOnError!

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

Similar Threads

  1. Open Edit Form with selected information
    By Ossos in forum Access
    Replies: 6
    Last Post: 10-02-2016, 09:06 AM
  2. Replies: 4
    Last Post: 05-23-2014, 12:22 PM
  3. VBA sql to edit specific information
    By Ruegen in forum Programming
    Replies: 1
    Last Post: 11-13-2013, 07:36 PM
  4. Add and edit Information in the same form?
    By sdel_nevo in forum Forms
    Replies: 3
    Last Post: 05-18-2013, 10:30 AM
  5. Replies: 1
    Last Post: 08-10-2012, 12:42 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