Solved

Access Convert string to date and query on date range

Posted on 2013-06-13
8
4,067 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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
 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

792 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