Need to get a "Days Old" working!

Posted on 2003-11-12
Last Modified: 2010-05-01
Hello Experts!

I'm using a datagrid to show information.  It's data comes from a few sql tables.  One table contains JRNL.DATE.  It shows its type as 'smalldatetime'.  It's formatted like this:  YYYY-MM-DD  TT:TT:TT

I need some help calculating how old each record is based on today's date.  

Do I need to go this route:

or is there something easier?
This will be used in an page.
Question by:jay-are
  • 5
  • 4

Expert Comment

ID: 9733954
Why not use the datediff function


Expert Comment

ID: 9733972
Example as given in MSDN

Dim TheDate As Date   ' Declare variables.
Dim Msg
TheDate = InputBox("Enter a date")
Msg = "Days from today: " & DateDiff("d", Now, TheDate)
MsgBox Msg

Jut substitute your stored date for 'TheDate' and you have your value.

Author Comment

ID: 9734474

So it should look something like this?

DateDiff("d", Now, [Jrnl.Date])?

I'm a noob to VB so you'll have to excuse my ignorance.
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 9738184
Hi Jay

Yes you're on the right lines, if you are extracting the date from a recordset value you may also need to enter the recordset name e.g.

MyValue = DateDiff("d", Now, Recordsetname![Jrnl.Date])

This should return the difference in days between the two dates.

Expert Comment

ID: 9739326
Use the sys clock to get the time when (installed i guess) and keep it in an ini file or something, then get the program to refer to it and the sysclock to see how many days there are, put this in teh app in something like

lblDaysGone = lalalalalala

Author Comment

ID: 9739752

I'm trying to use this in a datagrid.  I just need to display the days between each records date and today.  It actually needs to be done for every  control number in the dataset.  I've included my code here.  It gives me the error:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30203: Identifier expected.

Source Error:


Line 283:
Line 284:                   DataGrid1.Databind()
Line 285:                   Date1 = DateDiff("d", Now, objDataSet[Jrnl.Date])
Line 286:          End Sub
Line 287:

Source File: c:\inetpub\wwwroot\aspx\new\omg2.aspx    Line: 285

Can you help me get this working right with my code here?

Sub BindGrid()
                   Dim objRelation As DataRelation
                   Dim objDataSet As DataSet = New DataSet
                   Dim myConnection As New SqlConnection(ConnectionString)
                   Dim Adapter As SqlDataAdapter = New SqlDataAdapter
                   Dim Date1 As string

                   Adapter.SelectCommand = _
                       New SqlCommand(SelectCommand1, myConnection)

                      Adapter.SelectCommand = _
                       New SqlCommand(SelectCommand2, myConnection)

                   DataGrid1.DataSource = objDataSet.Tables("Accounting")

                   Date1 = DateDiff("d", Now, objDataSet[Jrnl.Date])
          End Sub



Expert Comment

ID: 9740592
Should there not be an exclamation mark in there after objDataSet ? i.e

Date1 = DateDiff("d", Now, objDataSet![Jrnl.Date])

Author Comment

ID: 9740634
I tried having the '!' after the objDataSet but it gives me this error:
 BC30277: Type character '!' does not match declared data type 'System.Data.DataSet'.

Accepted Solution

TomLaw1999 earned 250 total points
ID: 9740801
Sorry jay I am confident enough about datediff handling the calculation but I am a bit shaky with datasets. You will need to work out how to extract the date information then use it in the datediff function.

Author Comment

ID: 9740835
Thanks for your help Tom.  I'll post this up in the asp section and see what I get.

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
A macro to Count the number of rows across all worksheets 3 93
Visual Basic Excel Formatting error 4 101
VB6 Compile Compatibility Issue 4 102
How to debug this code 7 61
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now