Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access Convert string to date and query on date range

Posted on 2013-06-13
8
Medium Priority
?
5,541 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 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 52

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 52

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 52

Expert Comment

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

/gustav
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

783 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