Testing a DateField for age

# Thread: Testing a DateField for age

1. Junior Member
Join Date
Dec 1969
Posts
28

## Testing a DateField for age

Hi all, <BR><BR>I am working on a project similar where I need to do some testing on a date in the database. What I need to do is pull a set of records and display them, but if the orginial creation date (a field in the databse for each record) is older than 5 days old, I want to display the information in a read only format. This way the user can view the data but not edit it after it&#039;s 5 days old. I can post some of the code if it would help.<BR><BR>Thanks in advance. <BR>

2. Senior Member
Join Date
Dec 1969
Posts
1,316

## RE: Testing a DateField for age

Heres a handy function for this:<BR><BR>Function IsOlderThan(Date1, Date2, DaysToCheck)<BR> Dim blnResult: blnResult = False<BR> If IsDate(Date1) Then<BR> If IsDate(Date2) Then<BR> If CInt(Cdate(Date1) - CDate(Date2)) &#062;= CInt(DaysToCheck) Then<BR> blnResult = True<BR> End If<BR> End If<BR> End If<BR> IsOlderThan = blnResult<BR>End Function<BR><BR>So, if you wanted to see if the original creation date is older than 5 days old, you would do:<BR>If IsOlderThan(Now(), RecordSet("FieldName"), 6) Then<BR> &#039;It is 6 days or older<BR>Else<BR> &#039;It is exactly 5 days or newer<BR>End If

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## Do you want to...

...still put the info into &#060;INPUT&#062; form fields? Or just dump the data into the &#060;TD&#062;...&#060;/TD&#062; areas?<BR><BR>It&#039;s pretty easy, in any case:<BR><BR>Read only &#060;INPUT&#062; fields:<BR><BR>&#060;%<BR>SQL = "SELECT creationDate, .... FROM ..."<BR><BR>Set RS = yourConnection.Execute( SQL )<BR>Do Until RS.EOF<BR> created = RS("creationDate")<BR> isOld = created &#060; ( Date() - 5 ) &#039; true if more than 5 days old<BR> If isOld Then ro = " READONLY" Else ro = "" &#039; only used for &#060;INPUT&#062; version<BR>%&#062;<BR> &#060;TR&#062;<BR> &#060;TD&#062;&#060;INPUT Name="whatever" Value="&#060;%=RS("whatever")%&#062;" [hl="yellow"]&#060;%=ro%&#062;[/hl] &#062;&#060;/TD&#062;<BR> ...<BR> &#060;/TR&#062;<BR>&#060;%<BR> RS.MoveNext<BR>Loop<BR>%&#062;<BR><BR>************ ****<BR><BR>&#060;TD&#062; only for readonly stuff:<BR><BR>&#060;%<BR>SQL = "SELECT creationDate, .... FROM ..."<BR><BR>Set RS = yourConnection.Execute( SQL )<BR>Do Until RS.EOF<BR> created = RS("creationDate")<BR> isOld = created &#060; ( Date() - 5 ) &#039; true if more than 5 days old<BR> If isOld Then<BR>%&#062;<BR> &#060;TR&#062;<BR> &#060;TD&#062;&#060;%=RS("whatever"%&#062;&#060 ;/TD&#062;<BR> ...<BR> &#060;/TR&#062;<BR>&#060;%<BR> Else<BR>%&#062;<BR> &#060;TR&#062;<BR> &#060;TD&#062;&#060;INPUT Name="whatever" Value="&#060;%=RS("whatever")%&#062;" [hl="yellow"]&#060;%=ro%&#062;[/hl] &#062;&#060;/TD&#062;<BR> ...<BR> &#060;/TR&#062;<BR>&#060;%<BR> End If<BR> RS.MoveNext<BR>Loop<BR>%&#062;<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Minor problem with that...

...or, perhaps, an advantage to that, depending upon what&#039;s in your DB.<BR><BR>Since Jason used NOW(), he is getting the current *DATE AND TIME*. And then, when he does<BR> CInt(CDate(Date1) - CDate(Date2))<BR>he is subtracting the date in the DB from the current date and time and then *ROUNDING* the result to an even number of days.<BR><BR>Now, *IF* the value in the DB is *ONLY* a date, this means that Jason&#039;s code will actually be checking to see if it is equal or more than 6.5 (six and a half) days old. So you will get different results in the morning than in the afternoon.<BR><BR>Of course, *IF* the value in the DB is also a date AND TIME, then this code gives a more accurate result, in some sense. Though personally I would go for truncation (with INT) instead of rounding (with CINT).<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Actually...I guess I would...

...not use either INT or CINT.<BR><BR>The truncation serves no purpose.<BR><BR> 6.321 is still &#062;= 6<BR><BR>And if, perchance, the person really did want to check for 6.5 days, well...<BR><BR> 6.321 is still NOT &#062;= 6.5<BR><BR>So why bother with INT or CINT on either side of the test?<BR><BR>

6. Junior Member
Join Date
Dec 1969
Posts
28

## Thanks for the help...

Thanks for the help guys, I&#039;ll give it a shot.

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oops...goof in that...

Probably doesn&#039;t matter, but if you use the SECOND version, the &#060;%=ro%&#062; doesn&#039;t belong there. Because then the only INPUT fields will *NOT* be old and so will NOT be readonly.<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•