Solved

correct criteria for date using cdate

Posted on 2006-11-02
21
545 Views
Last Modified: 2010-01-22
I have a table with a text value for the date. In a query I have Date:cdate([txtDate]). I want to use a criteria so I'll see records that are dated less than a date field on a form. Can't seem to get the right criteria. This produces a error stating it's typed incorrectly or too complex or a data type mismatch if I leave out the DESC:

SELECT CDate([txtDate]) AS [Date]
FROM tblData
WHERE (((CDate([txtDate]))<[Forms]![frmStartUp]![txtToDate]))  (this is an unbound text box with format set to short date)
ORDER BY CDate([txtDate]) DESC;
0
Comment
Question by:avoorheis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +2
21 Comments
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17860149
SELECT CDate([txtDate]) AS [Date]
FROM tblData
WHERE (((CDate([txtDate]))< #[Forms]![frmStartUp]![txtToDate]#))  (this is an unbound text box with format set to short date)
ORDER BY CDate([txtDate]) DESC;

try that, the # signifies it as a date
0
 

Author Comment

by:avoorheis
ID: 17860192
won't let me just put in the #'s, also tried <"#" &[Forms]![frmStartUp]![txtToDate]&"#", but, didn't work either.
So, I tried just a fixed date, <#10/31/2006#, got a data type mismatch in criteria (after it briefly showed the data)  
0
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17860222
can you give me an example of the data that's in txtDate in tblData?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17860252
Very odd, I created a mock up of what you did.  I created a form called frmStartUp with an unbound textbox on that form with a format of short date.  I also created a table called tblData with a field called txtDate.  the field I defined was text and i put the following data in it:

1/1/2006
1-5-06
2/22/06
3/12/06
6/6/06

then I copied your query exactly into a new query and it worked....what is the data like in your table?
0
 

Author Comment

by:avoorheis
ID: 17860313
If I run the query without the criteria, I get:
9/1/2005, 8/1/2006 (all dates set to the first)
The data in the table looks like (when viewed in notepad, it's a linked table, to a csv file):
Oct 2006
0
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17860483
again very odd, I put in this data:

jan 2006
jun 2006
nov 2006
oct 2006
sept 2005

and it works fine.....make sure you've lost focus on the textbox in the form so that it saves the changes that you made...
0
 

Author Comment

by:avoorheis
ID: 17860556
yes, something seems strange. When I view the results of the query, I see the date as Nov-06. If I click on a date field, it changes to 11/1/2006. It is right justified, so, it knows it's not a text field.
0
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17860584
hrm....do you mind posting your database or a portion of it at www.ee-stuff.com ? if you do you have to change the extension to .txt b/c it won't accet .mdb files
0
 

Author Comment

by:avoorheis
ID: 17860656
it will take a while, I'm in the middle of  doing 3 things, and the database is very large, so, I'll make up something smaller.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17860664
You are supposed to zip it, not change extensions.  
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17860681
BTW, EE-Stuff.com has not been working for over 2 days.
0
 
LVL 6

Expert Comment

by:yhwhlivesinme
ID: 17860698
thanks GRayL, I forgot that you can put in zip files...
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17860778
In the design mode of the form, highlight the textbox with the date, rightclick Properties, format tab and beside Format enter -  mmm yyyy

Now when you enter a date, the system know it is to be a date, and you do not need the # delimiters.  You will also get an error message if your entry does not match any mmm yyyy format.
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17861722
Always end up being overly explicit when you don't store date type data in a date type field :-S

Your first solution looked almost spot on to me though
SELECT CDate([txtDate]) AS [Dated]
FROM tblData
WHERE CDate([txtDate])<CDate([Forms]![frmStartUp]![txtToDate])
ORDER BY CDate([txtDate]) DESC;

You shouldn't delimit an expression in a query - you'd only do that when passing a value, as the expression is evaluated by Access.  Delimiters won't help that.
e.g.
SELECT CDate([txtDate]) AS [Dated]
FROM tblData
WHERE CDate([txtDate])<#12/12/2006#  (this is an unbound text box with format set to short date)
ORDER BY CDate([txtDate]) DESC;
0
 

Author Comment

by:avoorheis
ID: 17861975
LPruvis,
I thought that would have worked too, but, didn't make any difference.
There must be something wacky with the txt file that I'm just not seeing.
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17862077
Text file?  (Or field?)

Can you provide some exact example data? (That is exemplary and exciting without being excruciating.... sorry :-)
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17862117
Sometimes forms become busted.  If it is not a lot of work, delete the old form and start from scratch.  
0
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 17873918
I think that nulls may be contributing to your problem (tested).  Give this a whirl:

SELECT iif (nz(txtDate,"") <> "", cdate([txtDate]), "") AS [dtDate]
FROM  t1
WHERE CDate(nz([txtDate],"1/1/1900")) <  [Forms]![frmStartUp]![txtToDate]
ORDER BY  CDate(nz([txtDate],"1/1/1900"));

1/1/1900 is an arbitrary early date to handle nulls in your txtDate.  If you want to exclude null txtDates from your query results revise the where clause like this:

WHERE CDate(nz([txtDate],"1/1/1900")) <  [Forms]![frmStartUp]![txtToDate]  AND nz([txtDate],"") <> ""
0
 

Author Comment

by:avoorheis
ID: 17873927
MB, you might be right, I forgot about that. I'll be working with this more early next week. Thanks.
0
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 17874144
You never did get us that example data... :-)  http:Q_22046927.html#17862077
But let's hypothesize...

Nulls could indeed be a problem.  (Yikes.. text fields acting as dates... *and* Nulls?  :-S)

And I wouldn't be surprised if the general problem was then with built in function calls from SQL (or indeed it could be argued, an issue of Jet SQL itself).
First of all - if there are null values - you wouldn't be able to exclude them in the same clause - e.g. And X Is Not Null.  
So potential for an error there still.
OK then - so a subquery - where they are fitlered first to remove the Nulls.
Perhaps surprisingly - you still can't be totally sure that an evaluation won't be carried out in the main query on a record which you might think it shouldn't even see.
"OK then" you think... "so I'll save a query which limits the results".
Nope - Jet is one step ahead of you (so it thinks) - and trying to limit the results returned even from a separate query, walking up the query tree.

So - to user defined function calls
For example...
Paste the following in a standard module.

Function fDateConv(varVal, Optional varAlt) As Date
    If IsDate(varVal) Then
        fDateConv = CDate(varVal)
    Else
        If Not IsMissing(varAlt) Then
            fDateConv = CDate(varAlt)
        End If
    End If
End Function

Then go with a query of...

SELECT CDate([txtDate]) AS [Dated]
FROM tblData
WHERE fDateConv([txtDate],[Forms]![frmStartUp]![txtToDate]) < [Forms]![frmStartUp]![txtToDate]
ORDER BY CDate([txtDate]) DESC;

Notice the use of the built in function everywhere except the Where clause.
Naturally - it's safe to do so by then.
0
 

Author Comment

by:avoorheis
ID: 17889744
So, there was a bad record in the data causing the problems.

Here is what happens when the data is ok:

cdate([month]).....does convert to date and sorts by date, and looks like 9/1/2006 (which I presume is the system format)
format(cdate([month]),"mmm-yy")....comes out in the format I want, but, sorts by alpha (by month name)
cdate([month]).....and setting the field properties format to mmm-yyy, in the query design view, shows the format I want and sorts by date.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

626 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