View Full Version : Calculating a date field in form


gtford
04-09-2002, 02:32 PM
I'm sure there is a simple answer to this, but I can't figure it out (obviously I'm new at this). Included in my form are three fields: ExDate, DaysOut and DueDate. I need to calculate the DueDate by adding the ExDate and DaysOut fields. I need the calculated DueDate to move into my data table in the DueDate field of the table.

Thanks.

Jack Cowley
04-09-2002, 02:40 PM
Take a look at the DateAdd() function in Help.

gtford
04-09-2002, 03:12 PM
Thanks, Jack. I had reviewed the DateAdd function, but wasn't certain how to use it in such a way that the calculated date would be reflected in the data table. I need the DueDate field bound to my DueDate field in the Data Table.

I also wasn't certain if the "number" part of the formula could be reflected as a field which contains a number.

For example, let's say my ExDate is 12/31/02 and my DaysOut is -160. I want the DueDate to be calulated by adding the DaysOut field and the ExDate field resulting in a DueDate of 7/24/02.

Thanks for your quick response to my first post.

David R
04-09-2002, 03:23 PM
Use the Form_BeforeUpdate event or something similar to plug the data into your table:
Private Sub Form_BeforeUpdate(Cancel as Integer)
If Not IsNull(Me.DueDateField) Then
Me.DueDateField = DateAdd("d",Me.ExDate,MeDaysOut)
End Sub


You could also use an Update Query to do a batch of records at the end of the day, instead of form by form. If you do it in the form, be sure you choose an event which will ALWAYS get triggered, or you'll end up with blanks.

HTH,
David R

Pat Hartman
04-09-2002, 05:31 PM
It would be far better to calculate the date in a query or on the form as you need it. Storing calculated data of this nature is considered poor practice since it violates second normal form.