Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access Convert string to date and query on date range

Posted on 2013-06-13
8
Medium Priority
?
5,165 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
[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
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 25

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 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
 

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 51

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 51

Expert Comment

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

/gustav
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

670 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