Subquery problem (1 Viewer)

medof

Registered User.
Local time
Today, 01:41
Joined
Sep 9, 2014
Messages
20
Hello

I have the attached database where i used a subquery to calculate the difference between the current record and the previous one

The problem that i can't add records to the form anymore ...

Is there a way to solve that ?? and if the answer is No ... is there any alternative method to get the same result ...?

Thanks
 

Attachments

  • difference in Query.mdb
    448 KB · Views: 100

MarkK

bit cruncher
Local time
Yesterday, 16:41
Joined
Mar 17, 2004
Messages
8,181
You can do something like . . .
 

Attachments

  • db2.zip
    51.1 KB · Views: 99

medof

Registered User.
Local time
Today, 01:41
Joined
Sep 9, 2014
Messages
20
wow .... great

Can i use the same code for Dates ? the Value field is Date

Code:
Option Compare Database
Option Explicit

Sub Recalculate()
    Dim rst As DAO.Recordset
    Dim dff As Long
    Dim nxt As Long
    Dim ttl As Long
    Dim Name As String
    Dim tot As Long
    
    Set rst = Me.RecordsetClone
    
    With CurrentDb.OpenRecordset( _
        "SELECT * FROM Table2 ORDER BY Name, Value" _
    )
        .MoveFirst
        Do While Not .EOF
            .Edit
            If Name <> !Name Then
                Name = !Name
                ttl = !Value
                !Next = Null
                !Diff = Null
                !Total = Null
            Else
                !Next = nxt
                !Diff = !Value - nxt
                ttl = ttl + !Value
                !Total = ttl
            End If
            .Update
            nxt = !Value
            .MoveNext
        Loop
        .Close
    End With
    Me.Recordset.Requery
    
End Sub

Private Sub Form_Load()
    Recalculate
End Sub
 

Users who are viewing this thread

Top Bottom