If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Compare two date Fields

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-26-12, 10:12
jsirico jsirico is offline
Registered User
 
Join Date: Jan 2012
Posts: 54
Compare two date Fields

Hi guys
I'd like to compare two Fields (Wich Stores Date) and than Add To a third Filed the Max(Field1,Field2)
this had looked to my a simple thing to do, but unfortunately working with date is more difficult than I'd expected;
I've tryed something like this:

Sub pi()
Dim rst As DAO.Recordset
Dim dif As Long
DTCMPT As DateTime
DTDEBEF As DateTime


Set rst = CurrentDb.OpenRecordset("Step1", dbOpenDynaset)
With rst
Do Until .EOF
dif = DateDiff(DTCMPT, DTDEBEF)
If dif <= 0 Then
.Edit
!MDATE = !DTCMPT
.Update
Else
.Edit
!MDATE = !DTDEBEF
.Update
End If
.MoveNext
Loop
.Close
End With
Set rst = Nothing
End Sub



End Sub

but of course it doesen't work becouse the syntax is wrong. How can I adjust this to work????
Thank you all for your answares!!!!
Reply With Quote
  #2 (permalink)  
Old 01-26-12, 10:30
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
This is incorrect:
Code:
dif = DateDiff(DTCMPT, DTDEBEF)
The proper syntax for DataDiff is:
Code:
DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])
See Access Help System.
__________________
Have a nice day!
Reply With Quote
  #3 (permalink)  
Old 01-26-12, 11:39
jsirico jsirico is offline
Registered User
 
Join Date: Jan 2012
Posts: 54
How can I declare that DTCMP , DTBEDEF, MDATE are the fields that I want to use to compare and update????

Last edited by jsirico; 01-26-12 at 12:02.
Reply With Quote
  #4 (permalink)  
Old 01-26-12, 12:58
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
Try:
Code:
Sub pi()

    Dim rst As DAO.Recordset
    Dim dif As Long
'
'    Dim DTCMPT As Date     ' Dim was missing for both lines,
'    Dim DTDEBEF As Date    ' the variable type is Date, not DateTime
                            ' Both lines are useless: DTCMPT  and DTDEBEF are not used.
'  
    Set rst = CurrentDb.OpenRecordset("Step1", dbOpenDynaset)
    With rst
        Do Until .EOF
            .Edit
            dif = DateDiff("d", !DTCMPT, !DTDEBEF)
            If dif <= 0 Then
                !MDATE = !DTCMPT
            Else
                !MDATE = !DTDEBEF
            End If
            .Update
            .MoveNext
        Loop
        .Close
    End With
    Set rst = Nothing
    
End Sub
__________________
Have a nice day!
Reply With Quote
  #5 (permalink)  
Old 01-26-12, 13:09
jsirico jsirico is offline
Registered User
 
Join Date: Jan 2012
Posts: 54
Thank you It Works Great
Reply With Quote
  #6 (permalink)  
Old 01-26-12, 14:13
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
in actual fact you can take advantage of the way that Access / JET stores dates whichis numeric the most significiant digits represenbt the number of days since (IIRC 01 Jan 1900), and the decimal = proportion of days eg .5 = 12:00, .75 = 18:00, 0.0416666666666667 an hour)

so you could run two queries
eg
update mytable set mydatecolumn = date1 where date1 >= date2
update mytable set mydatecolumn = date2 where date2 >date1

or
update mytable set mydatecolumn = iif(date1>= date2, date1, date2)


mind you it does beg the question why you'd want to store such derived data as you can always find the correct date in the query by using the iif clause above. storing derived data is a no no..
why?
what happens if later on somebody changes the values of one or more date columns, then you have to rerun your process to set the new date.

there are times when you can and should store what at first glance seems derived data. ferinstance on an order, the order details probably should list the price the goods were sold at, the prevailing sales tax rate (or value), the order header should record the actual customer. so you do not want to change those items if say your sales tax rate goes up, the supplier changes or changes name and so on.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #7 (permalink)  
Old 01-27-12, 04:03
jsirico jsirico is offline
Registered User
 
Join Date: Jan 2012
Posts: 54
Thank you sooooo much!!!!
GrazieMille!!!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On