Can I check Value in DAO Recordset field during update process?

marlan

I would like to end a loop of updating data in a table, by seeing some affect on a RecordSet.
In this case: Say I have Table1 with fields: GroupID, a Long, and Val, a Double. no Primary key no limit to the values referred to a GroupID, no limit to number of records for a single GroupID.

Dim ID as Long
Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("SELECT GroupID, SUM(Val) As ValSum FROM Table1 Order By SUM (Val) ASC Group By GroupID")
rst.MoveFirst
ID = rst.Fields(0)
Do while ID = rst.Fields(0)
    CurrentDB.Execute("INSERT INTO Table1 (GroupID, Val) VALUES (" & ID & ", 1)")
    rst.Requery()' Change value of first rst?...
Loop
'next line of code...

rst is first pointing at the GroupID with the lowest SUM(Val), I then add records witch should add to the value SUM(Val), and eventually have some other GroupID in the first record of rst. That should cause while ID = rst.Fields(0) to value False, and exit the loop.

in practice, I get an endless loop.

Is there a way to update value of a DAO.Recordset while running?

Andre

Are you trying to win a competition for the most confusing code or what...?

Anyway, your code works for me after correcting some errors (your SQL had wrong syntax, Val is a reserved word).

Sub TestConfusingRS()

    Dim ID As Long
    Dim S As String
    Dim rst As DAO.Recordset
    Dim i As Long

    S = "SELECT GroupID, SUM(myVal) As ValSum FROM Tabelle1 Group By GroupID Order By SUM(myVal) ASC"
    Set rst = CurrentDb.OpenRecordset(S)
    ID = rst.Fields(0)

    Do While ID = rst.Fields(0)
        i = i + 1

        Debug.Print i, "Inserting GroupID " & ID
        CurrentDb.Execute "INSERT INTO Tabelle1 (GroupID, myVal) VALUES (" & ID & ", 1)"
        rst.Requery

        ' Prevent endless loop
        If i > 100 Then Exit Do

    Loop

End Sub

With

+---------+-------+
| GroupID | myVal |
+---------+-------+
|       1 |     6 |
|       2 |     2 |
|       2 |     1 |
|       3 |     5 |
+---------+-------+

I get

 1            Inserting GroupID 2
 2            Inserting GroupID 2

and it stops here:

+---------+-------+
| GroupID | myVal |
+---------+-------+
|       1 |     6 |
|       2 |     2 |
|       2 |     1 |
|       2 |     1 |
|       2 |     1 |
|       3 |     5 |
+---------+-------+

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

During "make" process for AOSP build, Can I check a progress ratio?

From Dev

Check a recordset for an empty field

From Dev

Setting variable as a field reference in a DAO.Recordset

From Dev

What are difference between referencing a DAO Recordset field?

From Dev

How to access updated DAO recordset properties during a transaction?

From Dev

How can I create an update query that change the value of a field with the sysdate?

From Dev

How can I sum the sums of a recordset based on the value of a column?

From Dev

mongodb set field value using if else during document update

From Dev

How can I check if the speed value have been changed in the Update and then set the new speed value?

From Dev

Can I copy only one row/field from a recordset to an excel column

From Dev

ASP Classic check for database NULL value in Recordset

From Dev

Recordset Null Value not being detected in null check

From Dev

How can I check if a process is running in Linux?

From Dev

How can I check a process regarding to availability?

From Dev

Update certain column of recordset from textbox value

From Dev

How can I check a post-condition for a field's old value in D?

From Dev

How can I check if the input field is not empty

From Dev

Can I update value in a ListView?

From Dev

How can I update the value of a specific field of my table for all rows?

From Dev

How can I update any field value through VBA code in ms access?

From Dev

How can i update hidden field value on one page from the code behind of another page?

From Dev

How Can I Set Object from Dictionary to String Value to Update View Text Field?

From Dev

How can I get the lookup_field value in My Update APIView?

From Dev

jQuery: How can I update input field's value? (data from webSocket)

From Dev

Can I keep my app in Testflight during approval process

From Dev

How can i check if a particular process is a critical process?

From Dev

Using SQL how can I update field values of one column to match a different column's field value only if the values do NOT equal?

From Dev

Check field value, do a calculation (division) and update column values

From Dev

Access VBA - how can i convert a value in recordset from text to number?

Related Related

  1. 1

    During "make" process for AOSP build, Can I check a progress ratio?

  2. 2

    Check a recordset for an empty field

  3. 3

    Setting variable as a field reference in a DAO.Recordset

  4. 4

    What are difference between referencing a DAO Recordset field?

  5. 5

    How to access updated DAO recordset properties during a transaction?

  6. 6

    How can I create an update query that change the value of a field with the sysdate?

  7. 7

    How can I sum the sums of a recordset based on the value of a column?

  8. 8

    mongodb set field value using if else during document update

  9. 9

    How can I check if the speed value have been changed in the Update and then set the new speed value?

  10. 10

    Can I copy only one row/field from a recordset to an excel column

  11. 11

    ASP Classic check for database NULL value in Recordset

  12. 12

    Recordset Null Value not being detected in null check

  13. 13

    How can I check if a process is running in Linux?

  14. 14

    How can I check a process regarding to availability?

  15. 15

    Update certain column of recordset from textbox value

  16. 16

    How can I check a post-condition for a field's old value in D?

  17. 17

    How can I check if the input field is not empty

  18. 18

    Can I update value in a ListView?

  19. 19

    How can I update the value of a specific field of my table for all rows?

  20. 20

    How can I update any field value through VBA code in ms access?

  21. 21

    How can i update hidden field value on one page from the code behind of another page?

  22. 22

    How Can I Set Object from Dictionary to String Value to Update View Text Field?

  23. 23

    How can I get the lookup_field value in My Update APIView?

  24. 24

    jQuery: How can I update input field's value? (data from webSocket)

  25. 25

    Can I keep my app in Testflight during approval process

  26. 26

    How can i check if a particular process is a critical process?

  27. 27

    Using SQL how can I update field values of one column to match a different column's field value only if the values do NOT equal?

  28. 28

    Check field value, do a calculation (division) and update column values

  29. 29

    Access VBA - how can i convert a value in recordset from text to number?

HotTag

Archive