Solved

Access Convert string to date and query on date range

Posted on 2013-06-13
8
4,548 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

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
 

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 50

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 50

Expert Comment

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

/gustav
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

734 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