Similar to this question I have this error I'm not able to fix.
Here is the situation :
We have an application front-end under Access 2003 with linked tables from SQL Server 2008 R2.
We have a table Vehicule(immat, secteur) and a table Vehicule_suivi_km(immat, date, km). Our vehicles are stored in the [Vehicule] table with [immat] as primary key and their kilometer records stored in the [Vehicule_suivi_km] table with [immat]+[date] as primary key.
Now I have to create a summary of the activity of the year. I made it as following :
Query creation [MO km max 2016] :
SELECT Vehicule.immat, vsk2016.date, vsk2016.km
FROM Vehicule INNER JOIN Vehicule_suivi_km AS vsk2016 ON Vehicule.immat=vsk2016.immat
WHERE (((vsk2016.date)=(SELECT Max(date) FROM Vehicule_suivi_km WHERE (Vehicule_suivi_km.date<#01/01/17# And Vehicule_suivi_km.immat=vsk2016.immat))))
ORDER BY Vehicule.immat;
This code is giving me a line by vehicle with its most recent kilometer record before the #01/01/2017#.
I do it again for 2017 [MO km max 2017] :
SELECT Vehicule.immat, vsk2017.date, vsk2017.km
FROM Vehicule INNER JOIN Vehicule_suivi_km AS vsk2017 ON Vehicule.immat=vsk2017.immat
WHERE (((vsk2017.date)=(SELECT Max(date) FROM Vehicule_suivi_km WHERE (Vehicule_suivi_km.date<#01/01/18# And Vehicule_suivi_km.immat=vsk2017.immat))))
ORDER BY Vehicule.immat;
Now I want to create a query which join theses 2 tables and substract the kilometer of 2016 from the kilometer of 2017 [MO km this year] :
SELECT [MO km max 2017].immat, [MO km max 2017].km-Nz([MO km max 2016].km,0) AS km
FROM [MO km max 2017] LEFT JOIN [MO km max 2016] ON [MO km max 2017].immat = [MO km max 2016].immat;
I'm using a left join in case of we bought a vehicle after the 01/01/2017. So I'm using the Nz to transform the null values in 0 (in SQL Server it might by the COALESCE function but I'm using Access for querying)
I get the following error by executing this query (The multi-part identifier could not be bound [#4104]) :
If I use INNER JOIN instead of LEFT JOIN I don't get this error anymore but I would only have the records for vehicule bought before the 01/01/2017.
Is it a primary key problem ? I can't get what I'm doing wrong.
EDIT 1
The error message is saying
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server] The multi-part identifier "vsk2016.immat" could not be bound. 37000 4104 [Microsoft][ODBC Driver 11 for SQL Server][SQL Server] The multi-part identifier "vsk2016.date" could not be bound. (#4104)
EDIT 2
By adding a GROUP BY close in [MO km max 2016] and [MO km max 2017] it solved my problem :
SELECT Vehicule.immat, vsk2017.[date], vsk2017.km
FROM Vehicule INNER JOIN Vehicule_suivi_km AS vsk2017 ON Vehicule.immat=vsk2017.immat
WHERE (((vsk2017.[date])=(SELECT Max([date]) FROM Vehicule_suivi_km WHERE (Vehicule_suivi_km.[date]<#01/01/18# And Vehicule_suivi_km.immat=vsk2017.immat))))
GROUP BY Vehicule.immat, vsk2017.[date], vsk2017.km
ORDER BY Vehicule.immat;
But I still wondering why I got this error message. Does the engine wasn't sure that "immat" was unique in the result of my query so I had to add the GROUP BY close ?