Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / March 2007

Tip: Looking for answers? Try searching our database.

Date calculation, datatype mismatch

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Stephanie - 02 Mar 2007 17:43 GMT
Arg!
I have a date field in a table: MembershipLetterDate

I have a subquery on a different table that contains:
Membership Expiration: Max(DateAdd("d",365,[DateCreated]))

In a query, I'm trying to calculate the number of months between the
MembershipLetterDate and the Membership Expiration.

I refer to the subquery and try Diff2Dates:
MonthsToExp: Diff2Dates("m",[MembershipLetterDate],[Membership
Expiration],True)

And receive the datatype mismatch. These are both dates, although one is
calculated. Is that a problem? How can I fix it? Thanks- subqueries while
useful, usually trip me up.
Jerry Whittle - 02 Mar 2007 19:01 GMT
Unless you've created your own Diff2Dates function, I think you need the
DateDiff Function.
Signature

Jerry Whittle, Microsoft Access MVP
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.

> Arg!
> I have a date field in a table: MembershipLetterDate
[quoted text clipped - 12 lines]
> calculated. Is that a problem? How can I fix it? Thanks- subqueries while
> useful, usually trip me up.
Stephanie - 02 Mar 2007 19:13 GMT
Interesting!
I do have a Diff2Dates function and I use it all of the time so I forgot
about DateDiff. I still can't quite figure out what in my function wasn't
allowing the date calculation, but you are correct- DateDiff was the way to
go.
Thanks for solving my frustration!
Cheers!

> Unless you've created your own Diff2Dates function, I think you need the
> DateDiff Function.
[quoted text clipped - 15 lines]
> > calculated. Is that a problem? How can I fix it? Thanks- subqueries while
> > useful, usually trip me up.
KARL DEWEY - 02 Mar 2007 22:40 GMT
You can not use a calculated field name in the same query that does the
calculation.  You have to use the calculation instead.
Signature

KARL DEWEY
Build a little - Test a little

> Interesting!
> I do have a Diff2Dates function and I use it all of the time so I forgot
[quoted text clipped - 23 lines]
> > > calculated. Is that a problem? How can I fix it? Thanks- subqueries while
> > > useful, usually trip me up.
Stephanie - 03 Mar 2007 17:52 GMT
Ah! Thanks for that info. Queries are always challening for me so it's good
to know where I went wrong to avoid at least that issue on the next query! I
appreciate your help.

> You can not use a calculated field name in the same query that does the
> calculation.  You have to use the calculation instead.
[quoted text clipped - 26 lines]
> > > > calculated. Is that a problem? How can I fix it? Thanks- subqueries while
> > > > useful, usually trip me up.
Stephanie - 06 Mar 2007 22:49 GMT
Karl,
I was going to post a different question to the group, but I find that I
haven't solved this problem. I hope you have time to direct me.

Here's my subquery that I struggled over until the Discussion Group staged
an intervention(!):
SELECT Contacts.ContactID, Max(DuesLineItem.DateCreated) AS
MaxOfDateCreated, Max(DateAdd("d",365,[DateCreated])) AS [Membership
Expiration]
FROM Contacts INNER JOIN DuesLineItem ON Contacts.ContactID =
DuesLineItem.ContactID
GROUP BY Contacts.ContactID
HAVING (((Max(DateAdd("d",365,[DateCreated]))) Is Not Null));

I'll post the whole query (sorry and hope that's OK) and highlight the issue:
SELECT DISTINCT Contacts.ContactID, Contacts.LastName,
Val(Diff2Dates("y",[MemberDate],Date())) AS Years,
IIf(IsNull([NickName]),[FirstName] & " " & [LastName],[Nickname] & " " &
[LastName]) AS Name, IIf([AddressFlag]=1,[contacts].[MailingAddress1] & ",
"+[contacts].[OptAddress1],[contacts].[MailingAddress2] & ",
"+[contacts].[OptAddress2]) AS Address,
IIf([AddressFlag]=1,[contacts].[City1],[contacts].[City2]) AS City,
IIf([AddressFlag]=1,UCase([contacts].[StateOrProvince1]),UCase([contacts].[StateOrProvince2]))
AS State,
IIf([AddressFlag]=1,[contacts].[PostalCode1],[contacts].[PostalCode2]) AS
PostalCode, Contacts.MemberDate,
FldConcat3("AnimalNB2","Animal","ContactID",[Contacts].[ContactID]) AS
[Animal List], [Renewal subquery].[Membership Expiration],
Val(Diff2Dates("m",[Membership Expiration],Date())) AS DueDate,
Contacts.MembershipLetterDate,
DateDiff("m",[MembershipLetterDate],[Membership Expiration],True) AS
MonthsToExp
FROM (Contacts INNER JOIN MemberStatus ON Contacts.MemberStatusID =
MemberStatus.MemberStatusID) INNER JOIN [Renewal subquery] ON
Contacts.ContactID = [Renewal subquery].ContactID
GROUP BY Contacts.ContactID, Contacts.LastName,
Val(Diff2Dates("y",[MemberDate],Date())), IIf(IsNull([NickName]),[FirstName]
& " " & [LastName],[Nickname] & " " & [LastName]),
IIf([AddressFlag]=1,[contacts].[MailingAddress1] & ",
"+[contacts].[OptAddress1],[contacts].[MailingAddress2] & ",
"+[contacts].[OptAddress2]),
IIf([AddressFlag]=1,[contacts].[City1],[contacts].[City2]),
IIf([AddressFlag]=1,UCase([contacts].[StateOrProvince1]),UCase([contacts].[StateOrProvince2])),
IIf([AddressFlag]=1,[contacts].[PostalCode1],[contacts].[PostalCode2]),
Contacts.MemberDate,
FldConcat3("AnimalNB2","Animal","ContactID",[Contacts].[ContactID]), [Renewal
subquery].[Membership Expiration], Val(Diff2Dates("m",[Membership
Expiration],Date())), Contacts.MembershipLetterDate,
DateDiff("m",[MembershipLetterDate],[Membership Expiration],True),
Contacts.MemberOption, MemberStatus.MemberStatus
HAVING (((Val(Diff2Dates("m",[Membership Expiration],Date()))) Between 0 And
1) AND ((Contacts.MemberOption)=1) AND
((MemberStatus.MemberStatus)="Current-Active")) OR
(((Val(Diff2Dates("m",[Membership Expiration],Date()))) Between 0 And 1) AND
((Contacts.MemberOption)=1) AND
((MemberStatus.MemberStatus)="current-Active"))
ORDER BY Val(Diff2Dates("y",[MemberDate],Date()));

The issue:
DateDiff("m",[MembershipLetterDate],[Membership Expiration],True)
You told me that I can't use [Membership Expiration] since it's a calculated
field
but I can't seem to get this:
Max(DateAdd("d",365,[DateCreated]))

to work in the datediff calculation. How can I write it so that I can get:
DateDiff("m",[MembershipLetterDate],[Membership Expiration],True) AS
MonthsToExp

I'll try to explain what I'm ultimately trying to do as it may affect how I
need the query criterion (and I'll be glad to post this question as a new
one)- I want to add a MembershipLetterDate field so that one month before a
membership is due, I send out a renewal letter (flagging the
MembershipLetterDate field as today's date). Every year, I'll want to send
the letter one month before the expiration date. So if I use the statement
"between 0 and 1", that doesn't take into consideration the SerialDate.  I'll
want to see if the MembershipLetterDate is blank or within 11 months of
expiration. To append the MembershipLetterDate, I have code I'll modify:
strWhere = "[MembershipLetterDate] Is Null Or [MembershipLetterDate] <
DateSerial(Year(Date()),1,1)"  then I'm not sure how to change the
DateSerial...

Sorry to dump- one step at a time. The question at hand is how to handle
DateDiff("m",[MembershipLetterDate],[Membership Expiration],True) AS
MonthsToExp

with Max(DateAdd("d",365,[DateCreated])) AS [Membership Expiration]

I appreciate your time if you have any to spare ;-)
Cheers!

> You can not use a calculated field name in the same query that does the
> calculation.  You have to use the calculation instead.
[quoted text clipped - 26 lines]
> > > > calculated. Is that a problem? How can I fix it? Thanks- subqueries while
> > > > useful, usually trip me up.
KARL DEWEY - 06 Mar 2007 23:34 GMT
I would recommend you break your query into several smaller ones that use the
earlier query.  This way you will not have the problem. The second query CAN
use the calculated field alias to do further calculations.
Signature

KARL DEWEY
Build a little - Test a little

> Karl,
> I was going to post a different question to the group, but I find that I
[quoted text clipped - 117 lines]
> > > > > calculated. Is that a problem? How can I fix it? Thanks- subqueries while
> > > > > useful, usually trip me up.
Stephanie - 08 Mar 2007 19:13 GMT
Thanks for the reply.

> I would recommend you break your query into several smaller ones that use the
> earlier query.  This way you will not have the problem. The second query CAN
[quoted text clipped - 121 lines]
> > > > > > calculated. Is that a problem? How can I fix it? Thanks- subqueries while
> > > > > > useful, usually trip me up.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.