Copy Excel Formula to Access

P

Palacelaw

In Excel I have this formula and it works like a charm for cell M2.

=IF(ISBLANK(C2),"",IF(ISBLANK(L2),DATEDIF(C2,TODAY(),"y") & " years, " &
DATEDIF(C2,TODAY(),"ym") & " months, " & DATEDIF(C2,TODAY(),"md") & "
days",DATEDIF(C2,L2,"y") & " years, " & DATEDIF(C2,L2,"ym") & " months, " &
DATEDIF(C2,L2,"md") & " days"))

I have the same three fields in Access but I have no idea how to copy the
formula for the field I created for M2.

I am really new to access so any help is greatly appreciated.

Thanks
 
K

Ken Snell \(MVP\)

ACCESS does not allow formulas in "cells". You'd need to use a query that
would contain a calculated field in which a calculation is done (like the
formula) to give you the desired result.

I'm not familiar with a worksheet function named DATEDIF for EXCEL, so I
cannot give a suggestion for what the calculated field's expression would
be. Perhaps you can explain to us what the EXCEL formula is doing?
 
J

John W. Vinson

In Excel I have this formula and it works like a charm for cell M2.

=IF(ISBLANK(C2),"",IF(ISBLANK(L2),DATEDIF(C2,TODAY(),"y") & " years, " &
DATEDIF(C2,TODAY(),"ym") & " months, " & DATEDIF(C2,TODAY(),"md") & "
days",DATEDIF(C2,L2,"y") & " years, " & DATEDIF(C2,L2,"ym") & " months, " &
DATEDIF(C2,L2,"md") & " days"))

I have the same three fields in Access but I have no idea how to copy the
formula for the field I created for M2.

I am really new to access so any help is greatly appreciated.

Thanks

Excel is a spreadsheet program, best of breed.

Access is a relational database development environment, a good one.

They are DIFFERENT. Access is *not* "Excel on steroids"; an Access Table is
NOT a spreadsheet, and cannot contain formulas or expressions.

You can use expressions (slightly different in syntax, often) in Access
Queries to do calculations on fields stored in a table; e.g. it's DateDiff not
DATEDIF, Date() instead of TODAY(), and the syntax is different.

Perhaps you could explain what you're trying to accomplish; I'm sure Access
can do it, but I am even more sure that it will do it in a different way than
Excel would!
 
P

Palacelaw

In Excel:

C2 = Open Date
L2 = Date Completed
M2 = Number of Open Days (expressed in Y, M, D format)

M2 calculates a value based on the values in C2 and L2. If C2 is blank then
M2 is blank. If C2 has a value then the formula looks to the contents of L2
to calculate a value for M2. If L2 is blank then M2 populates with the
difference (in Y, M, D format) between today’s date and the value in C2. If
L2 has a value then M2 populates with the difference (in Y, M, D format)
between C2 and L2.

In Access:

I created a table that has among other things fields called Open Date, Date
Completed, and Number of Open Days. I then created a form based on this
table. In the form once the user puts a value in the Open Date field I want
Number of Open Days to automatically populate value based on the same logic I
used in Excel.

Does that make sense? Thanks again for your help. I really appreciate it.
 
K

Ken Snell \(MVP\)

You don't really want to store the value for Number of Open Days in your
table. That value can always be calculated from the other two fields (Open
Date and Date Completed) via a query, and you can display the results of
that calculation on the form.

So, eliminate the Number of Open Days field from your table.

There are two ways to do the calculation -- one involves adding a calculated
field to the query that you're using as the form's RecordSource; the other
involves using an expression in a textbox on the form. My preference is to
do it in a query.

First, put the following function (taken from
http://www.tek-tips.com/faqs.cfm?fid=3928) into a regular module in your
database (be sure to name the module something other than DateDiff2; perhaps
name the module basCode):

' ** START OF CODE
Public Function DateDiff2(ByVal d1 As Date, ByVal d2 As Date) As String
Dim d3 As Date, nDays As Integer
Dim d1Day As Integer, nDayStart As Integer
Dim nYears As Integer, d1Mon As Integer, MonLast As Integer, nn As Integer,
nMon As Integer, d1Year As Integer

If d1 > d2 Then
d3 = d1
d1 = d2
d2 = d3
End If

d1Day = Day(d1)
d1Mon = Month(d1)
d1Year = Year(d1)
nn = 0
nMon = 0
nDays = 0
nYears = 0

MonLast = d1Mon
Do While d2 > d1
d1 = d1 + 1
nn = nn + 1

nDays = nDays + 1

If (Day(d1) = d1Day) And Month(d1) <> MonLast Then
nMon = nMon + 1
nDays = 0
MonLast = Month(d1)
If nMon = 12 Then
nMon = 0
nYears = nYears + 1
End If
End If
Loop


DateDiff2 = "Years: " & Format(nYears, "00") & " Months: " & Format(nMon,
"00") & " Days: " & Format(nDays, "00")
End Function
' ** END OF CODE


Assuming that you are using a query as the form's RecordSource, go to that
query and open it in design view (NOTE: if you're using the table name as
the form's RecordSource, then create a query that is based on that table,
and continue with what I'm suggesting here).

In the first empty column, put the following expression in the Field: box:

[Number of Open Days]: IIf([Open Date] Is Null, Null, IIf([Date Completed]
Is Null, DateDiff2([Open Date], Date()), DateDiff2([Date Completed], [Open
Date]))

Save and close the query.

Then, on your form, put [Number of Open Days] in the ControlSource property
of the textbox that is to display the "Number of Open Days".

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/
 
P

Palacelaw

Thanks for the info. I'm with you up to this point:
In the first empty column, put the following expression in the Field: box:

[Number of Open Days]: IIf([Open Date] Is Null, Null, IIf([Date Completed]
Is Null, DateDiff2([Open Date], Date()), DateDiff2([Date Completed], [Open
Date]))

Save and close the query.

I copied and pasted that expression in the Field box as you described and
got this error message: The expression you entered is missing a closing
parens, bracket, or vertical bar. I think there is a missing parens
somewhere but I'm not sure where it should go.

Thanks again.

I copied

Ken Snell (MVP) said:
You don't really want to store the value for Number of Open Days in your
table. That value can always be calculated from the other two fields (Open
Date and Date Completed) via a query, and you can display the results of
that calculation on the form.

So, eliminate the Number of Open Days field from your table.

There are two ways to do the calculation -- one involves adding a calculated
field to the query that you're using as the form's RecordSource; the other
involves using an expression in a textbox on the form. My preference is to
do it in a query.

First, put the following function (taken from
http://www.tek-tips.com/faqs.cfm?fid=3928) into a regular module in your
database (be sure to name the module something other than DateDiff2; perhaps
name the module basCode):

' ** START OF CODE
Public Function DateDiff2(ByVal d1 As Date, ByVal d2 As Date) As String
Dim d3 As Date, nDays As Integer
Dim d1Day As Integer, nDayStart As Integer
Dim nYears As Integer, d1Mon As Integer, MonLast As Integer, nn As Integer,
nMon As Integer, d1Year As Integer

If d1 > d2 Then
d3 = d1
d1 = d2
d2 = d3
End If

d1Day = Day(d1)
d1Mon = Month(d1)
d1Year = Year(d1)
nn = 0
nMon = 0
nDays = 0
nYears = 0

MonLast = d1Mon
Do While d2 > d1
d1 = d1 + 1
nn = nn + 1

nDays = nDays + 1

If (Day(d1) = d1Day) And Month(d1) <> MonLast Then
nMon = nMon + 1
nDays = 0
MonLast = Month(d1)
If nMon = 12 Then
nMon = 0
nYears = nYears + 1
End If
End If
Loop


DateDiff2 = "Years: " & Format(nYears, "00") & " Months: " & Format(nMon,
"00") & " Days: " & Format(nDays, "00")
End Function
' ** END OF CODE


Assuming that you are using a query as the form's RecordSource, go to that
query and open it in design view (NOTE: if you're using the table name as
the form's RecordSource, then create a query that is based on that table,
and continue with what I'm suggesting here).

In the first empty column, put the following expression in the Field: box:

[Number of Open Days]: IIf([Open Date] Is Null, Null, IIf([Date Completed]
Is Null, DateDiff2([Open Date], Date()), DateDiff2([Date Completed], [Open
Date]))

Save and close the query.

Then, on your form, put [Number of Open Days] in the ControlSource property
of the textbox that is to display the "Number of Open Days".

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/




Palacelaw said:
In Excel:

C2 = Open Date
L2 = Date Completed
M2 = Number of Open Days (expressed in Y, M, D format)

M2 calculates a value based on the values in C2 and L2. If C2 is blank
then
M2 is blank. If C2 has a value then the formula looks to the contents of
L2
to calculate a value for M2. If L2 is blank then M2 populates with the
difference (in Y, M, D format) between today's date and the value in C2.
If
L2 has a value then M2 populates with the difference (in Y, M, D format)
between C2 and L2.

In Access:

I created a table that has among other things fields called Open Date,
Date
Completed, and Number of Open Days. I then created a form based on this
table. In the form once the user puts a value in the Open Date field I
want
Number of Open Days to automatically populate value based on the same
logic I
used in Excel.

Does that make sense? Thanks again for your help. I really appreciate
it.
 
K

Ken Snell \(MVP\)

Yes, I omitted one parenthesis.

[Number of Open Days]: IIf([Open Date] Is Null, Null, IIf([Date Completed]
Is Null, DateDiff2([Open Date], Date()), DateDiff2([Date Completed], [Open
Date])))

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/


Palacelaw said:
Thanks for the info. I'm with you up to this point:
In the first empty column, put the following expression in the Field:
box:

[Number of Open Days]: IIf([Open Date] Is Null, Null, IIf([Date
Completed]
Is Null, DateDiff2([Open Date], Date()), DateDiff2([Date Completed],
[Open
Date]))

Save and close the query.

I copied and pasted that expression in the Field box as you described and
got this error message: The expression you entered is missing a closing
parens, bracket, or vertical bar. I think there is a missing parens
somewhere but I'm not sure where it should go.

Thanks again.

I copied

Ken Snell (MVP) said:
You don't really want to store the value for Number of Open Days in your
table. That value can always be calculated from the other two fields
(Open
Date and Date Completed) via a query, and you can display the results of
that calculation on the form.

So, eliminate the Number of Open Days field from your table.

There are two ways to do the calculation -- one involves adding a
calculated
field to the query that you're using as the form's RecordSource; the
other
involves using an expression in a textbox on the form. My preference is
to
do it in a query.

First, put the following function (taken from
http://www.tek-tips.com/faqs.cfm?fid=3928) into a regular module in your
database (be sure to name the module something other than DateDiff2;
perhaps
name the module basCode):

' ** START OF CODE
Public Function DateDiff2(ByVal d1 As Date, ByVal d2 As Date) As String
Dim d3 As Date, nDays As Integer
Dim d1Day As Integer, nDayStart As Integer
Dim nYears As Integer, d1Mon As Integer, MonLast As Integer, nn As
Integer,
nMon As Integer, d1Year As Integer

If d1 > d2 Then
d3 = d1
d1 = d2
d2 = d3
End If

d1Day = Day(d1)
d1Mon = Month(d1)
d1Year = Year(d1)
nn = 0
nMon = 0
nDays = 0
nYears = 0

MonLast = d1Mon
Do While d2 > d1
d1 = d1 + 1
nn = nn + 1

nDays = nDays + 1

If (Day(d1) = d1Day) And Month(d1) <> MonLast Then
nMon = nMon + 1
nDays = 0
MonLast = Month(d1)
If nMon = 12 Then
nMon = 0
nYears = nYears + 1
End If
End If
Loop


DateDiff2 = "Years: " & Format(nYears, "00") & " Months: " &
Format(nMon,
"00") & " Days: " & Format(nDays, "00")
End Function
' ** END OF CODE


Assuming that you are using a query as the form's RecordSource, go to
that
query and open it in design view (NOTE: if you're using the table name as
the form's RecordSource, then create a query that is based on that table,
and continue with what I'm suggesting here).

In the first empty column, put the following expression in the Field:
box:

[Number of Open Days]: IIf([Open Date] Is Null, Null, IIf([Date
Completed]
Is Null, DateDiff2([Open Date], Date()), DateDiff2([Date Completed],
[Open
Date]))

Save and close the query.

Then, on your form, put [Number of Open Days] in the ControlSource
property
of the textbox that is to display the "Number of Open Days".

--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/




Palacelaw said:
In Excel:

C2 = Open Date
L2 = Date Completed
M2 = Number of Open Days (expressed in Y, M, D format)

M2 calculates a value based on the values in C2 and L2. If C2 is blank
then
M2 is blank. If C2 has a value then the formula looks to the contents
of
L2
to calculate a value for M2. If L2 is blank then M2 populates with the
difference (in Y, M, D format) between today's date and the value in
C2.
If
L2 has a value then M2 populates with the difference (in Y, M, D
format)
between C2 and L2.

In Access:

I created a table that has among other things fields called Open Date,
Date
Completed, and Number of Open Days. I then created a form based on
this
table. In the form once the user puts a value in the Open Date field I
want
Number of Open Days to automatically populate value based on the same
logic I
used in Excel.

Does that make sense? Thanks again for your help. I really appreciate
it.


:

ACCESS does not allow formulas in "cells". You'd need to use a query
that
would contain a calculated field in which a calculation is done (like
the
formula) to give you the desired result.

I'm not familiar with a worksheet function named DATEDIF for EXCEL, so
I
cannot give a suggestion for what the calculated field's expression
would
be. Perhaps you can explain to us what the EXCEL formula is doing?
--

Ken Snell
<MS ACCESS MVP>
http://www.accessmvp.com/KDSnell/



In Excel I have this formula and it works like a charm for cell M2.

=IF(ISBLANK(C2),"",IF(ISBLANK(L2),DATEDIF(C2,TODAY(),"y") & " years,
"
&
DATEDIF(C2,TODAY(),"ym") & " months, " & DATEDIF(C2,TODAY(),"md") &
"
days",DATEDIF(C2,L2,"y") & " years, " & DATEDIF(C2,L2,"ym") & "
months,
"
&
DATEDIF(C2,L2,"md") & " days"))

I have the same three fields in Access but I have no idea how to
copy
the
formula for the field I created for M2.

I am really new to access so any help is greatly appreciated.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Expanding Date formula 1
A mathematical formula question??? 3
Averaging 6
Excel formula calculating age 4
Add/Sum up months, years, dates 4
Age computation 1
Birthday stuff 21
Calculate age with an end date 3

Top