how to calculate DateDiff in Access form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear All,

How to get the below date difference code in VB working for Access

Private Sub Command1_Click()
Dim y, w As Integer
y = (Date - CDate(Text1.Text)) \ 365
w = (Date - CDate(Text1.Text)) - y * 365
w = w \ 7
d = DateDiff("d", CDate(Text1.Text), Date)
MsgBox "Years" & Str(y) & "week" & Str(w)
End Sub

It works fine in VB, though not in access..

I need to know the how many years and weeks passed from date entered till
current date i.e. today.

Any help will be appreciated

Thanks

Gerald
 
Gerald said:
How to get the below date difference code in VB working for Access

Private Sub Command1_Click()
Dim y, w As Integer
y = (Date - CDate(Text1.Text)) \ 365
w = (Date - CDate(Text1.Text)) - y * 365
w = w \ 7
d = DateDiff("d", CDate(Text1.Text), Date)
MsgBox "Years" & Str(y) & "week" & Str(w)
End Sub

It works fine in VB, though not in access..

I need to know the how many years and weeks passed from date entered till
current date i.e. today.


Access does not use the Text property the same way VB does.
You need to use the Value property intstead, but since it's
the default property for controls, you don't need to
explicitly refer to it. I.e. just get rid of the .Text in
your references.

OTOH, why not use DateDiff for all this:

y = DateDiff("yyyy", Text1, Date)
w = DateDiff("ww", Text1, Date) - 52 * y

but then, maybe your code is not supposed to be aware of
leap years.

You may, or may not, be interested in this procedure:
http://www.accessmvp.com/djsteele/Diff2Dates.html
 
Back
Top