Solved

Access Convert string to date and query on date range

Posted on 2013-06-13
8
3,364 Views
Last Modified: 2013-06-14
my table has a string date in the form of YYYYMMDD

I am using query designer to format the string to a date and then need to query on a date range.

All of the following convert the string to the format that I need, but then when I try to plug in a date for the criteria, I get "Data type mismatch .."

Date1: CDate(Left([FileDate],4) & " / " & Mid([FileDate],5,2) & " / " & Mid([FileDate],7,2))

Date2: Format(CDate(Left([FileDate],4) & " / " & Mid([FileDate],5,2) & " / " & Mid([FileDate],7,2)))

Date3: DateValue(Format([FileDate],"@@@@\/@@\/@@"))

For the criteria, I have tried:

#05/13/2013#
"#05/13/2013#"
"05/13/2013"
"5/13/2013"

I am using Access 2007
0
Comment
Question by:Delta7428
8 Comments
 
LVL 4

Expert Comment

by:BAKADY
ID: 39246019
first change this
Date2: Format(CDate(Left([FileDate],4) & " / " & Mid([FileDate],5,2) & " / " & Mid([FileDate],7,2)))

Open in new window

to this
Date2: CDate(Mid([FileDate],5,2) & " / " & Mid([FileDate],7,2)  & " / " &  Left([FileDate],4))

Open in new window

You want to work with date datatypes, right?
0
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 39246091
This might be a problem with the field data type -- is it a Date field?  I like to save converted Date values to database properties, after checking that they are valid dates, then use the properties in a Date Range criterion, like this:

Between CDate(GetProperty("FromDate","")) And CDate(GetProperty("ToDate",""))
0
 
LVL 24

Expert Comment

by:chaau
ID: 39246265
To check if the value is a proper date, you need to use IsDate function.

Between Iif(IsDate(GetProperty("FromDate","")), CDate(GetProperty("FromDate","")), #01/01/1900#) And Iif(IsDate(GetProperty("ToDate","")), CDate(GetProperty("ToDate","")), #01/01/2015#)

Open in new window


See, how I used Iif together with IsDate to fall back to some predefined default dates when the values are not date values
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 39246735
If you use:

  RealFileDate: CDate(Format([FileDate],"@@@@/@@/@@"))

the expression will return values of data type Date which you can and must filter with a date expression:

  #05/13/2013#

If some are empty (Null), you can use:

  RealFileDate: CVDate(Format([FileDate],"@@@@/@@/@@"))

However, if some of your text dates are invalid, like 20130631, it will fail.
You can check that with:

  IsRealDate: IsDate(Format([FileDate],"@@@@/@@/@@"))

If not all are true, you can either filter for that or adjust the conversion:

  RealFileDate: CVDate(IIf(IsDate(Format([FileDate],"@@@@/@@/@@")), Format([FileDate],"@@@@/@@/@@"),Null))

Please note the use of CVDate which accepts Null values.

/gustav
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Delta7428
ID: 39247731
cactus_data, I believe you are the only one that read my post

I am working with a small dataset for testing.  The only date (STRING TYPE) in my test table is 20130513.  No nulls.  No invalid dates.

I created a field in query designer according to your solution:
RealFileDate: CDate(Format([FileDate],"@@@@/@@/@@"))

Then entered #05/13/2013# for the selection criteria.

I still got "data type mismatch in criteria expression"
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39247819
So if you run the Query with no filtering, the column RealFileDate will return true dates?

If so, something else must be going on.
Any chance you could upload a sample that fails?

/gustav
0
 

Author Comment

by:Delta7428
ID: 39248242
Yes, RealFileDate returns true dates.

I discovered the problem was with the text file that was being used as a linked table.  (Sorry forgot to mention the data source was not an Access table.)

In the process of creating a test database to attach here and using a clone of the text file format, I discovered that your RealFileDate formula works.  So there is an issue with the particular file I was linked to.

Points coming.

Thanks.  :)
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39248308
OK, that explains. Have a nice weekend!

/gustav
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…

706 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

15 Experts available now in Live!

Get 1:1 Help Now