Jakub Bidziński
Jakub Bidziński

Reputation: 143

MS ACCESS - the query works through a query, and there is an error in the VBA. How to modify the query?

I have written a query that is not executed in VBA code, but the same query works through a query in MS Access. How do I modify it?

Code which is working:

SELECT SLO_portfele.P_nazwaPortfela from ((SLO_portfele INNER JOIN Ewidencje on SLO_portfele.ID_portfela = Ewidencje.ID_portfela) INNER JOIN KP_KartyProjektow on KP_KartyProjektow.ID_kartyProjektu = Ewidencje.ID_kartyProjektu) WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = "Generowanie CWU"

Code which is not working:

strSql = "SELECT SLO_portfele.P_nazwaPortfela from SLO_portfele INNER JOIN Ewidencje on SLO_portfele.ID_portfela = Ewidencje.ID_portfela INNER JOIN KP_KartyProjektow on KP_KartyProjektow.ID_kartyProjektu = Ewidencje.ID_kartyProjektu WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu) & "' "
Set rst = CurrentDb.OpenRecordset(strSql)
przypisanie5 = rst!P_nazwaPortfela
rst.Close

However, this is strange, because another query in VBA works (similar):

strSql = "SELECT Ewidencje.E_dataPlanowaneZakonczenieProjektu from Ewidencje INNER JOIN KP_KartyProjektow on Ewidencje.ID_kartyProjektu = KP_KartyProjektow.ID_kartyProjektu WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu) & "' "
Set rst = CurrentDb.OpenRecordset(strSql)
przypisanie2 = rst!E_dataPlanowaneZakonczenieProjektu
rst.Close

The mistake that comes up is..: This code generates a message error:

Syntax error (no operator).

How do I process the query?

Upvotes: 0

Views: 94

Answers (1)

Gustav
Gustav

Reputation: 55806

Print the finished SQL:

strSql = "SELECT SLO_portfele.P_nazwaPortfela from SLO_portfele INNER JOIN Ewidencje on SLO_portfele.ID_portfela = Ewidencje.ID_portfela INNER JOIN KP_KartyProjektow on KP_KartyProjektow.ID_kartyProjektu = Ewidencje.ID_kartyProjektu WHERE KP_KartyProjektow.KP_krotkaNazwaProjektu = '" & (krotkaNazwaProjektu) & "' "
Debug.Print strSQL

Then open a new query and, in SQL view, copy-paste the content of strSQL.

Run the query and you will see why it is "not working".

Upvotes: 2

Related Questions