datediff between first and last row in subform (1 Viewer)

falcondeer

Registered User.
Local time
Today, 14:15
Joined
May 12, 2013
Messages
101
Hi

Can somebody help me to calculate the datediff between the first and last row in a subform as shown in the picture.
I want to calculate the difference between the first session (Initial Assessment) 2.9.2020 and the last session (DC) 8.9.2020.
The goal is to know how many days the patient stayed in Physical therapy.

Thanks
 

Attachments

  • Pic 1.jpg
    Pic 1.jpg
    210.9 KB · Views: 179

Gasman

Enthusiastic Amateur
Local time
Today, 22:15
Joined
Sep 21, 2011
Messages
14,299
I would use the recordsetclone of the form.
When first opened save the date in that record as firstdate
Then MoveLast and save the date in that record as lastdate
Then subtract one from the other and adjust depending on whether you include a day or not.?

HTH
 

falcondeer

Registered User.
Local time
Today, 14:15
Joined
May 12, 2013
Messages
101
I would use the recordsetclone of the form.
When first opened save the date in that record as firstdate
Then MoveLast and save the date in that record as lastdate
Then subtract one from the other and adjust depending on whether you include a day or not.?

HTH
Thanks for the quick reply, in fact I am not expert in this, so could you please show me how to write the code.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:15
Joined
Sep 21, 2011
Messages
14,299
You will need to adjust for your form and control names.
I just quickly created a form and added this code.

You also need to allow for subform reference, http://www.fmsinc.com/free/NewTips/Access/accesstip26.asp

Code:
Private Sub cmdCalcsDays_Click()
Dim rst As DAO.Recordset
Dim iDays As Integer
Dim dtStart As Date, dtEnd As Date

Set rst = Me.RecordsetClone
dtStart = rst.Fields("TransactionDate")
rst.MoveLast
dtEnd = rst.Fields("TransactionDate")
Me.txtDays = dtEnd - dtStart

Set rst = Nothing
End Sub

HTH
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:15
Joined
Feb 19, 2002
Messages
43,275
Make sure that the subform is sorted in the order that will make first and last meaningful to the calculation.
 

falcondeer

Registered User.
Local time
Today, 14:15
Joined
May 12, 2013
Messages
101
You will need to adjust for your form and control names.
I just quickly created a form and added this code.

You also need to allow for subform reference, http://www.fmsinc.com/free/NewTips/Access/accesstip26.asp

Code:
Private Sub cmdCalcsDays_Click()
Dim rst As DAO.Recordset
Dim iDays As Integer
Dim dtStart As Date, dtEnd As Date

Set rst = Me.RecordsetClone
dtStart = rst.Fields("TransactionDate")
rst.MoveLast
dtEnd = rst.Fields("TransactionDate")
Me.txtDays = dtEnd - dtStart

Set rst = Nothing
End Sub

HTH
Thanks a lot Gasman.
 

vhung

Member
Local time
Today, 14:15
Joined
Jul 8, 2020
Messages
235
Can somebody help me to calculate the datediff between the first and last row in a subform as shown in the picture.
I want to calculate the difference between the first session (Initial Assessment) 2.9.2020 and the last session (DC) 8.9.2020.
The goal is to know how many days the patient stayed in Physical therapy.
several ways to do it
>try this way
>use query to sql
SELECT [PhysicalTherapy].groupyear, First([PhysicalTherapy].sessiondate) AS FirstOfdate, Last([PhysicalTherapy].sessiondate) AS LastOfdate, [LastOfdate]-[FirstOfdate] AS diffdays
FROM PhysicalTherapy
GROUP BY [PhysicalTherapy].groupyear;
>but have to add field as groupyear = 2020, for this year
>see the TableQuery attachment below
>might use msgbox to view result
 

Attachments

  • daysdiff.png
    daysdiff.png
    313.1 KB · Views: 168

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:15
Joined
Feb 19, 2002
Messages
43,275
vhung,
First() and Last() are not relevant concepts in a relational database. What is first and what is last? Tables and queries are defined as UNORDERED sets so first or last is always indeterminate. However, if you are working with a SORTED recordset, you can order the data into a meaningful sequence and as you scroll forward and backward, the order will remain constant as it does with a flat file and so therefore, predictable. Domain functions do not work with ordered sets and so never return predictable First/last records. Now, you're going to go out to your 20 row table and you are going to try prove me wrong. You're going to think that the expected record is ALWAYS returned. Once you start working with large recordsets and live data which is actually updated, you might see the issue or you can just read the MS documentation which will give you the same warning.

The reason people don't see this problem in testing is because their recordsets are too small, and none of the updated records have moved from the spot they were originally inserted in. The biggest reason Access developers are fooled by this is because of the compact process. One of the functions of a compact is to reorder all records back int physical sequence by PK.

FYI, Here's a link to an article that explains the issue in detail.
 

vhung

Member
Local time
Today, 14:15
Joined
Jul 8, 2020
Messages
235
vhung,
First() and Last() are not relevant concepts in a relational database. What is first and what is last? Tables and queries are defined as UNORDERED sets so first or last is always indeterminate. However, if you are working with a SORTED recordset, you can order the data into a meaningful sequence and as you scroll forward and backward, the order will remain constant as it does with a flat file and so therefore, predictable. Domain functions do not work with ordered sets and so never return predictable First/last records. Now, you're going to go out to your 20 row table and you are going to try prove me wrong. You're going to think that the expected record is ALWAYS returned. Once you start working with large recordsets and live data which is actually updated, you might see the issue or you can just read the MS documentation which will give you the same warning.

The reason people don't see this problem in testing is because their recordsets are too small, and none of the updated records have moved from the spot they were originally inserted in. The biggest reason Access developers are fooled by this is because of the compact process. One of the functions of a compact is to reorder all records back int physical sequence by PK.

FYI, Here's a link to an article that explains the issue in detail.
well if that would be, so let it be
>i guess yes "recordsets are too small"
 

falcondeer

Registered User.
Local time
Today, 14:15
Joined
May 12, 2013
Messages
101
You will need to adjust for your form and control names.
I just quickly created a form and added this code.

You also need to allow for subform reference, http://www.fmsinc.com/free/NewTips/Access/accesstip26.asp

Code:
Private Sub cmdCalcsDays_Click()
Dim rst As DAO.Recordset
Dim iDays As Integer
Dim dtStart As Date, dtEnd As Date

Set rst = Me.RecordsetClone
dtStart = rst.Fields("TransactionDate")
rst.MoveLast
dtEnd = rst.Fields("TransactionDate")
Me.txtDays = dtEnd - dtStart

Set rst = Nothing
End Sub

HTH
I know this is might be weird but I just tried this code today and it is not working with me,

The following is what I did:

Private Sub cmdCalcsDays_Click()

Dim rst As DAO.Recordset
Dim iDays As Integer
Dim dtStart As Date
Dim dtEnd As Date
Set rst = Me.RecordsetClone

dtStart = Forms![tblPatient]![tblPhysiotherapy Subform].Form![PT_SessionDate]
rst.MoveLast
dtEnd = Forms![tblPatient]![tblPhysiotherapy Subform].Form![PT_SessionDate]

Me.txtDays = dtEnd - dtStart

Set rst = Nothing

End Sub

When I put the cursor on both dtStart and dtEnd it reads "12:00:00 AM" , and the result in the text box is "0"

Any idea what's wrong

Thanks
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:15
Joined
May 7, 2009
Messages
19,242
don't use Forms![tblPatient]![tblPhysiotherapy Subform].Form
remember, you are using Recodset, just use

dtStart = rst![PT_SessionDate]
rst.MoveLast
dtEnd = rst![PT_SessionDate]
Me.txtDays = DateDiff("d", dtStart, dtEnd)
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:15
Joined
Sep 21, 2011
Messages
14,299
I know this is might be weird but I just tried this code today and it is not working with me,

The following is what I did:

Private Sub cmdCalcsDays_Click()

Dim rst As DAO.Recordset
Dim iDays As Integer
Dim dtStart As Date
Dim dtEnd As Date
Set rst = Me.RecordsetClone

dtStart = Forms![tblPatient]![tblPhysiotherapy Subform].Form![PT_SessionDate]
rst.MoveLast
dtEnd = Forms![tblPatient]![tblPhysiotherapy Subform].Form![PT_SessionDate]

Me.txtDays = dtEnd - dtStart

Set rst = Nothing

End Sub

When I put the cursor on both dtStart and dtEnd it reads "12:00:00 AM" , and the result in the text box is "0"

Any idea what's wrong

Thanks
Yes, you are using the same field for both, hence 0. :(
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:15
Joined
Feb 19, 2002
Messages
43,275
I forgot to mention in my reply to vhung that simply changing First() to Min() and Last() to Max() would actually work. First and Last are totally dependent on row order and can be different from one execution to the next. However, Min and Max are precise and do not produce different results depending on the order of the recordset.
 

Cronk

Registered User.
Local time
Tomorrow, 07:15
Joined
Jul 4, 2013
Messages
2,772
@falcondeer wrote:
I know this is might be weird but I just tried this code today and it is not working with me,

The following is what I did:

Private Sub cmdCalcsDays_Click()
.....
Set rst = Me.RecordsetClone
If that code is for a control on the main form, it will be opening a recordset based on that form's recordsource. To refer to the subform's recordset, you need
Code:
Set rst = Me![tblPhysiotherapy Subform].Form.RecordsetClone
 

falcondeer

Registered User.
Local time
Today, 14:15
Joined
May 12, 2013
Messages
101
don't use Forms![tblPatient]![tblPhysiotherapy Subform].Form
remember, you are using Recodset, just use

dtStart = rst![PT_SessionDate]
rst.MoveLast
dtEnd = rst![PT_SessionDate]
Me.txtDays = DateDiff("d", dtStart, dtEnd)
Hi

Same problem, I still get "0"

See pic.

Thanks
 

Attachments

  • pic 3B.png
    pic 3B.png
    124.9 KB · Views: 132
Last edited:

falcondeer

Registered User.
Local time
Today, 14:15
Joined
May 12, 2013
Messages
101
If that code is for a control on the main form, it will be opening a recordset based on that form's recordsource. To refer to the subform's recordset, you need
Code:
Set rst = Me![tblPhysiotherapy Subform].Form.RecordsetClone
I used it but same thing, I get "12:00:00 AM" in dtStart and dtEnd.
See pic..

Thanks
 

Attachments

  • Pic 3A.png
    Pic 3A.png
    110 KB · Views: 101

Cronk

Registered User.
Local time
Tomorrow, 07:15
Joined
Jul 4, 2013
Messages
2,772
In what form do dtStart and dtEnd are located? In which event and in which form is the code running? Does the code actually run?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:15
Joined
May 7, 2009
Messages
19,242
Code:
Private Sub cmdCalcDays_Click()
    Dim rst As DAO.Recordset
    Dim iDays As Integer
    Dim dtStart As Date
    Dim dtEnd As Date
    
    dtStart = #1/1/9999#
    dtEnd = #12/31/1899#

    Set rst = Forms![tblPatient]![TblPhysiotherapy Subform].Form.RecodsetClone
    ' the following code will work whether the recordset is unsorted/sorted desc or asc on PT_SessionDate
    With rst
        If .RecordCount > 0 Then
            .MoveFirst
            Do Until .EOF
                dtStart = IIF(![PT_SessionDate] <= dtStart, ![PT_SessionDate], dtStart)
                dtEnd = IIF(![PT_SessionDate] >= dtEnd, ![PT_SessionDate], dtEnd)
                .MoveNext
            Loop
            iDays = DateDiff("d", dtStart, dtEnd)
        End If
    End With
    Set rst = Nothing
    Me![txtDays] = iDays
End Sub
 

falcondeer

Registered User.
Local time
Today, 14:15
Joined
May 12, 2013
Messages
101
In what form do dtStart and dtEnd are located? In which event and in which form is the code running? Does the code actually run?
dtStart and dtEnd are two variable declared in the VB code which reads the first row and last row of "PT_SessionDate" field in the sub-sub form which is located in the main form.
 

Users who are viewing this thread

Top Bottom