473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting first and last date from records

reginaldmerritt
201 100+
Hello

I'm have a form displaying records from a table holding details of events.

I'm using DFirst and DLast to work out what is the First and Last date.

I have the following code which simply writes this information on a message box after updating the field "Event Date"

Expand|Select|Wrap|Line Numbers
  1. Private Sub EventDate_AfterUpdate()
  2.  
  3. MsgBox ("First Date = " & DFirst("EventDate", "TBBookedEvents") & _
  4.         vbNewLine & "Last Date = " & DLast("EventDate", "TBBookedEvents"))
  5.  
  6. End Sub
  7.  
Its a bit hard to explaine but the problem i have is that the code is kinda showing the OldValue rather than the new value just updated.

So if i have the first date as 01/02/11 and then change this to 02/02/11 the message box shows 01/02/11. but then if i change the first date again this time to 03/02/11 the message box shows the 02/02/11.

Strange things a happening, any idea what i need to change to get this to work properly. Any suggestions welcome.

P.S. tried using DMin and DMax instead but had the same issue.
Feb 26 '11 #1

✓ answered by Stewart Ross

Hi. Problem is that the modified record that is underneath the form's controls is still in edit mode. The changes made have not yet been stored, so at the time you are running your domain aggregates DMax and DMin these pick up the previously-stored values.

The requery method you inserted stores the pending change to the record before requerying the form, hence you find that DMax and DMin work correctly.

A simpler approach is to test the form's Dirty property (i.e. record has been changed but not stored), and reset this property (i.e. save the record before proceeding):

Expand|Select|Wrap|Line Numbers
  1. Private Sub CalcStartEndDate() 
  2.  
  3. If Me.Dirty then
  4.   Me.Dirty = False
  5. End If
  6.  
  7. 'Update a field on the main form 
  8. Forms!FRMBookingDetails.StartDate = DMin("EventDate", "TBBookedEvents",_ 
  9.       "[BookingID] = Forms!FRMBookingDetails.BookingID") 
  10.  
  11. 'Update a field on the main form 
  12. Forms!FRMBookingDetails.EndDate = DMax("EventDate", "TBBookedEvents",_ 
  13.       "[BookingID] = Forms!FRMBookingDetails.BookingID") 
  14.  
  15. End Sub
If this sub is not on the form concerned then you would need to qualify the reference to the Dirty property:

Expand|Select|Wrap|Line Numbers
  1. If Forms!FRMBookingDetails.Dirty then
  2.   Forms!FRMBookingDetails.Dirty = False
  3. End If
-Stewart

3 2734
reginaldmerritt
201 100+
Well i suppose it did only take me 4hrs to sovle.

I was getting the same effect on a main form when updating a sub form. I tried all sorts to get this to work. I noticed that when deleting a record on a sub form via a piece of code that runs a delete query that the field i was trying to update updated.

This has to be something to do with focus so something but basically i have to requery the subform which the code is running before running the code. Don't fully understand why this is or why this works. But hey its 4:10am now and i'm just happy i can go to sleep.

Here is my actual code, hope this can be of help to someone else:

Expand|Select|Wrap|Line Numbers
  1. Private Sub CalcStartEndDate()
  2.  
  3. 'Have to requery this subform for some reason ???
  4. Forms!FRMBookingDetails.BookedEventsSubForm.Form.Requery
  5.  
  6. 'Update a field on the main form
  7. Forms!FRMBookingDetails.StartDate = DMin("EventDate", "TBBookedEvents",_
  8.       "[BookingID] = Forms!FRMBookingDetails.BookingID")
  9.  
  10. 'Update a field on the main form
  11. Forms!FRMBookingDetails.EndDate = DMax("EventDate", "TBBookedEvents",_
  12.       "[BookingID] = Forms!FRMBookingDetails.BookingID")
  13.  
  14. End Sub
  15.  
Feb 26 '11 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi. Problem is that the modified record that is underneath the form's controls is still in edit mode. The changes made have not yet been stored, so at the time you are running your domain aggregates DMax and DMin these pick up the previously-stored values.

The requery method you inserted stores the pending change to the record before requerying the form, hence you find that DMax and DMin work correctly.

A simpler approach is to test the form's Dirty property (i.e. record has been changed but not stored), and reset this property (i.e. save the record before proceeding):

Expand|Select|Wrap|Line Numbers
  1. Private Sub CalcStartEndDate() 
  2.  
  3. If Me.Dirty then
  4.   Me.Dirty = False
  5. End If
  6.  
  7. 'Update a field on the main form 
  8. Forms!FRMBookingDetails.StartDate = DMin("EventDate", "TBBookedEvents",_ 
  9.       "[BookingID] = Forms!FRMBookingDetails.BookingID") 
  10.  
  11. 'Update a field on the main form 
  12. Forms!FRMBookingDetails.EndDate = DMax("EventDate", "TBBookedEvents",_ 
  13.       "[BookingID] = Forms!FRMBookingDetails.BookingID") 
  14.  
  15. End Sub
If this sub is not on the form concerned then you would need to qualify the reference to the Dirty property:

Expand|Select|Wrap|Line Numbers
  1. If Forms!FRMBookingDetails.Dirty then
  2.   Forms!FRMBookingDetails.Dirty = False
  3. End If
-Stewart
Feb 26 '11 #3
reginaldmerritt
201 100+
I understand what you mean. Thanks for your input Steward, much appreciated.
Feb 26 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: siatki | last post by:
Hello, Firstly, sorry for my english. I have problem with creating SQL statement. I am beginner and I think that it is very easy to do. Look - I have to get only the last 20 records from table...
20
by: Guru | last post by:
Hi I have a table which contains number of rows. I want to fetch the last 5 records from the table. I know for the first 'n' records we can use FETCH FIRST n ROWS ONLY command. But i want to...
7
by: Drygast | last post by:
I have a form where I would like to get the todays date and when the date is 15 or less I would like to assign a textfield the last date of previous month and when the date is larger then 15 I...
0
by: Lee Harr | last post by:
I wrote a function to return the first date of a given week (and a few related functions) : -- return the first date in the given week CREATE or REPLACE FUNCTION week_start(integer, integer)...
6
by: Christo | last post by:
I have this script for showing news on a page, but i want it to only show the last 10 records, as in the 10 records that were added to the database last. the script shows the entries in descending...
9
by: ice | last post by:
Hello, I have a couple of tables. The client tables and the contacted tables. I am not sure how to start on this, what I need is a way to query all my clients then show any client that the...
1
by: BurtonBach | last post by:
I have a query that selects any records in a table that have to do with units ordered. I would like to have a nested query that then selects the last three records from the above query. Here is...
2
oszapped
by: oszapped | last post by:
I am new to creating SQL queries and need some help. I am trying to query from three tables. 1 - A table with a list of Drawings (DRAWINGS) 2 - A table with the Revision history of the Drawings...
8
by: Dr Al | last post by:
I have a table with four date fields, some of which may not be filled in based on our data entry needs. I have a criteria set as <date()-180 which is supposed to pull dates older than 180 days ago....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.