The following did not work. I'm getting the same run time error: Data type mismatch.
So, I assume it's a syntax error. Please note that the final variable (oid) is a Long data type.
Main Topics
Browse All TopicsI have a SQL statement that is working:
sql = "select * from tblOrderDetail Where tblOrderDetail.DetailID = (select MAX(DetailID) from tblOrderDetail)"
I would like to add an additional condition, the OrderID field must match.
dim oid as Long
oid = Me.OrderID.Value
I have tried the following but it's not working;
sql = "select * from tblOrderDetail Where tblOrderDetail.DetailID = (select MAX(DetailID) from tblOrderDetail) AND tblOrderDetail.OrderID = ('" & oid & "')"
Any suggestions?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
oid is a long, so you want to write OrderID=123, not Order='123'. But that is not the real problem.
You select the last DetailID, and then filter for a given OrderID. If perchance the last detail belongs to the order you want, you are fine, if not, you get no record.
You want the last Detail for a given order. That would be:
SELECT TOP 1 * FROM tblOrderDetail
WHERE OrderID=123
ORDER BY DetailID DESC
Or, in VB:
sql _
= " SELECT TOP 1 *" _
& " FROM tblOrderDetail" _
& " WHERE OrderID=" & oid _
& " ORDER BY DetailID DESC"
Good luck!
(°v°)
>> Data type mismatch.
AND tblOrderDetail.OrderID = ('" & oid & "')"
Why are you appending a single quote ' before and after your oid variable.
Single quotes before and after '1' makes this as a string instead of 1
Kindly remove single quotes and Open and Close braces so that it look like
AND tblOrderDetail.OrderID = " & oid
Business Accounts
Answer for Membership
by: rrjegan17Posted on 2009-10-22 at 05:42:21ID: 25633402
>> I have tried the following but it's not working;
Syntax is correct. Kindly explain what you meant by not working either not fetching results or error while executing or ...
And try this one out..
select * from tblOrderDetail
Where DetailID = (select MAX(DetailID) from tblOrderDetail)
AND OrderID = ('" & oid & "')"