ID | DateVal | ||||||
1 | 1/01/22 | ||||||
2 | 1/02/22 | ||||||
3 | 1/03/22 | ||||||
4 | 1/04/22 | ||||||
5 | 1/05/22 |
ID | DateVal | ||||||
1 | 1/01/22 | ||||||
2 | 1/02/22 | ||||||
3 | 1/15/22 | ||||||
4 | 1/04/22 | ||||||
5 | 1/05/22 |
Dim previousValue
Private Sub txtExample_BeforeUpdate(Cancel As Integer)
previousValue = txtExample.OldValue
End Sub
YES.Not really possible to solve with only one piece of sample data. According to your post, if you have
ID DateVal 1 1/01/22 2 1/02/22 3 1/03/22 4 1/04/22 5 1/05/22
the previous to the last "saved" record value is likely 01/04/2022 - sometimes. However, if I edit the records thus:
then the last saved record is ID 3 so the previous value is 01/02/2022.
ID DateVal 1 1/01/22 2 1/02/22 3 1/15/22 4 1/04/22 5 1/05/22
Regardless, you will probably have to create a sub query. How that would look depends on what it is you exactly want.
If Me.NewRecord Then
txtDate = DLookup("[DateVal]", "[Table Name Here]", "ID = " & Me.ID - 1)
End If
thanks for your effortIf
- primary key field of form record is named ID
- form control that gets the value you want is named txtDate
- table date field is named DateVal
then try using the form AfterUpdate event as per code below. This would only work where a new record is being created, which is what you seem to be asking for:
VBA Code:If Me.NewRecord Then txtDate = DLookup("[DateVal]", "[Table Name Here]", "ID = " & Me.ID - 1) End If
SAME errorI just remembered there is no guarantee that the prior ID value is one less than the record being added. This should be safer to use:
txtDate = DMax("[DateVal]", "[Table Name Here]", "ID < " & Me.ID)
What you really want is the Max of the value that is less than the value just added.
Edit - Saw that you had posted just after I hit save. Will look at what you posted now.