Datatype mismatch when ordering or searching criteria in field | Newbie | | Join Date: Oct 2009 Location: Memphis, TN
Posts: 11
| |
I am working in Access 2007 attempting to grab 2 characters from a text field named ProjectNumber. After determining what 2 digits to add in front to make it a year, I need to do that. I have done that in the code below to create a value for " intPN". I then need that value to become a number so that I can compare it to a range of years (1983-1987). The code below returns the 4 character year as intended. But I get a Datatype mismatch in criteria expression error whenever I try to order or use a critera (where) based on that field. I have been playing with this code for 2 days and am ready to go postal if the Datatype mismatch error pops up too many more times. So any help would really be appreciated. It is probably something really simple and I am too thick to see it. Thanks. - SELECT ArcFilz.ProjectNumber,
-
IIf(Len([ProjectNumber])>=7 And [ProjectNumber] Not Like "*P*" And [ProjectNumber] Not Like "*S*",
-
IIf(CInt(Mid([ProjectNumber],3,2))>80,
-
CInt("19"+Mid([ProjectNumber],3,2)),
-
CInt("20"+Mid([ProjectNumber],3,2))),
-
1960) AS intPN
-
FROM ArcFilz;
| |
best answer - posted by NeoPa |
An alternative set (less tidy as two different lengths aren't checked easily) to reflect the 7 or 9 digit spec : -
SELECT ProjectNumber,
-
intPN
-
-
FROM (SELECT ProjectNumber,
-
IIf(Len([ProjectNumber]) In(7,9) And [ProjectNumber] Like '#######*',
-
CInt('19' & Mid([ProjectNumber],3,2)) + IIf(Mid([ProjectNumber],3,2)>'80',0,100),
-
1960) AS intPN
-
FROM ArcFilz) AS subQry
-
-
WHERE intPN Between 1983 And 1987
-
-
ORDER BY intPN
| | Expert | | Join Date: Jul 2008 Location: Maryland
Posts: 1,175
| | | re: Datatype mismatch when ordering or searching criteria in field
Are you trying to compare numbers to dates? I can't tell without seeing where the result is being used.
| | Newbie | | Join Date: Oct 2009 Location: Memphis, TN
Posts: 11
| | | re: Datatype mismatch when ordering or searching criteria in field
Actually I am comparing to a number. So that I could say this: - WHERE intPN BETWEEN 1984 and 1988
But I can't even do this:
without the Datatype mismatch error.
Thanks.
| | Expert | | Join Date: Jul 2008 Location: Maryland
Posts: 1,175
| | | re: Datatype mismatch when ordering or searching criteria in field
The only way I have been able to create an error with this is when the call to mid returns a letter.
| | Newbie | | Join Date: Oct 2009 Location: Memphis, TN
Posts: 11
| | | re: Datatype mismatch when ordering or searching criteria in field
That is interesting. I've tried it on a different computer and get the same results whenever I try to order or filter the data by the intPN field. So I think I've ruled out the Access application as the problem. There is no problem with the ProjectNumber field, just intPN.
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,727
| | | re: Datatype mismatch when ordering or searching criteria in field
You cannot use the alias (intPN) of a formula from your SELECT clause in either the ORDER BY or WHERE clauses in Jet SQL.
If you try to design this in the QBE window you will find it easier. Access will use the formula in the ORDER BY & WHERE clauses for you.
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,727
| | | re: Datatype mismatch when ordering or searching criteria in field
You may consider a little simplification of the code : - SELECT ArcFilz.ProjectNumber,
-
IIf(Len([ProjectNumber])>=7 And [ProjectNumber] Not Like '*[PS]*',
-
CInt('19' & Mid([ProjectNumber],3,2)) + IIf(Mid([ProjectNumber],3,2)>'80',0,100,
-
1960) AS intPN
-
FROM ArcFilz
See ANSI Standards in String Comparisons for an explanation of the filtering.
If you were to post some example data it may be possible to simplify it further (or direct it more precisely maybe).
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,727
| | | re: Datatype mismatch when ordering or searching criteria in field
You could of course, encapsulate the complicated formula into a subquery (See Subqueries in SQL) and thereby make including it in the WHERE and ORDER BY clauses much more straightforward : - SELECT ProjectNumber,
-
intPN
-
-
FROM (SELECT ProjectNumber,
-
IIf(Len([ProjectNumber])>=7 And [ProjectNumber] Not Like '*[PS]*',
-
CInt('19' & Mid([ProjectNumber],3,2)) +
-
IIf(Mid([ProjectNumber],3,2)>'80',0,100,
-
1960) AS intPN
-
FROM ArcFilz) AS subQry
-
-
WHERE intPN Between 1983 And 1987
-
-
ORDER BY intPN
| | Newbie | | Join Date: Oct 2009 Location: Memphis, TN
Posts: 11
| | | re: Datatype mismatch when ordering or searching criteria in field
I really appreciate your help. I don't know how to do this without using the alias. To simplify what I am doing you could have a table with just an auto ID field and a ProjectNumber field (text) and a Project Name field (text) with the data in the attached spreadsheet:
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,727
| | | re: Datatype mismatch when ordering or searching criteria in field
I'm a little confused. Maybe I need to explain myself better : - I just want some example data posted of the sorts of things that could be found in the [ProjectNumber] field. An explanation of the form it takes wouldn't hurt if you can too.
- It's not necessary to do it as an attachment (in fact it's far better not), but if it must be then 2007 format is not very standard. I myself don't have it, so I can't even open what you've attached.
- The example data needn't be overly voluminous. 20 or 30 lines would be more than enough. Even fewer if the format is quite standard.
- I'm curious that you say you don't know how to do it without the aliases. I thought I'd posted an example (using a subquery) that showed exactly how you could. Does that not work for you?
| | Newbie | | Join Date: Oct 2009 Location: Memphis, TN
Posts: 11
| | | re: Datatype mismatch when ordering or searching criteria in field
I really do appreciate your help. I did try the subquery, but got an error in Access when I tried it. "Syntax error in FROM clause". I have not done a lot with subqueries. Sorry about the Access 2007 thing. Here is the data. As you can see some of the project numbers are shorter, some have letters in them, and some are even blank. These do not fit the pattern and I want to basically set the year of all those to 1960 as there will be no filters looking for those dates since they are before our company was formed. So basically I am throwing them out for now, but I might look at them in a different way later. Also any number pair that is over 80 should have a 19 apended to the front and lower numbers should have a 20 appended before them. That is how I am dealing with the 20th and 21st century issues. I hope this is clearer. - 1 0S9011000 Project 1
-
2 009014100 Project 2
-
3 009629000 Project 3
-
4 Project 4
-
5 010016200 Project 5
-
6 009103900 Project 6
-
7 008500700 Project 7
-
8 008806600 Project 8
-
9 008911200 Project 9
-
10 009203800 Project 10
-
11 030103112 Project 11
-
12 010203400 Project 12
-
13 020005902 Project 13
-
14 009709700 Project 14
-
15 P148500 Project 15
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,727
| | | re: Datatype mismatch when ordering or searching criteria in field
Would it be true to say that all the valid [ProjectNumber]s are 9 digits long and with the YY value at positions #3 & #4? If so then we can simplify the selection somewhat. Let me know.
Could you post the actual SQL you tried that gave the error "Syntax error in FROM clause" please. I've rechecked what I posted and I can't see anything wrong there.
| | Newbie | | Join Date: Oct 2009 Location: Memphis, TN
Posts: 11
| | | re: Datatype mismatch when ordering or searching criteria in field
All the valid YY values are in positions #3 & #4, but some valid Project Numbers may have only 7 digits. So they would be either 7 or 9 digits in length. I don't think any have 8.
Thanks again.
Here is the code. I just copied and pasted it into the Access SQL window. - SELECT ProjectNumber,
-
intPN
-
-
FROM (SELECT ProjectNumber,
-
IIf(Len([ProjectNumber])>=7 And [ProjectNumber] Not Like '*[PS]*',
-
CInt('19' & Mid([ProjectNumber],3,2)) +
-
IIf(Mid([ProjectNumber],3,2)>'80',0,100,
-
1960) AS intPN
-
FROM ArcFilz) AS subQry
-
-
WHERE intPN Between 1983 And 1987
-
-
ORDER BY intPN
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,727
| | | re: Datatype mismatch when ordering or searching criteria in field
Ah, My bad.
I left a closing parenthesis off the end of line #6.
It should have been : - IIf(Mid([ProjectNumber],3,2)>'80',0,100),
|  | Administrator | | Join Date: Oct 2006 Location: London - UK
Posts: 15,727
| | | re: Datatype mismatch when ordering or searching criteria in field
An alternative set (less tidy as two different lengths aren't checked easily) to reflect the 7 or 9 digit spec : -
SELECT ProjectNumber,
-
intPN
-
-
FROM (SELECT ProjectNumber,
-
IIf(Len([ProjectNumber]) In(7,9) And [ProjectNumber] Like '#######*',
-
CInt('19' & Mid([ProjectNumber],3,2)) + IIf(Mid([ProjectNumber],3,2)>'80',0,100),
-
1960) AS intPN
-
FROM ArcFilz) AS subQry
-
-
WHERE intPN Between 1983 And 1987
-
-
ORDER BY intPN
|  | Similar Microsoft Access / VBA bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|