I have a working app using access frontend and MS SqlServer backend (sqlexpress). My intention was to replace sqlexpress with mysql backend, so I migrated the tables using mysql workbench and relinked them in the access frontend. I was expecting many problems in the code, but this I don't understand.
In the original vba code I use querydef with parameters to insert data into the tables, as follows, and it works without problems.
Code:
Dim dbsRicevute As DAO.Database
.....
Set dbsRicevute = CurrentDb
Set qdfric = dbsRicevute.CreateQueryDef("", "PARAMETERS p1 INT, p2 DateTime, p3 Text, p4 Float, p5 Text, p6 Text, p7 Text, p8 Text; " & _
"INSERT INTO Ricevute (Numero, Data, Da, Totale, INLETTERE, Descrizione, [Note], MetodoPagamento) " & _
"VALUES (p1, p2, p3, p4, p5, p6, p7, p8);")
Set qdfprimanota = dbsRicevute.CreateQueryDef("", "PARAMETERS p1 float, p2 float, p3 Text, p4 Datetime, p5 Float, p6 Float, p7 Text, p8 Text; " & _
"INSERT INTO PrimaNota (Ricevuta, [Conto N], Descrizione, Data, Entrate, Uscite, [Note], Mese) " & _
"VALUES (p1, p2, p3, p4, p5, p6, p7, p8);")
....
With qdfric
!p1 = NumRec
!p2 = NData
!p3 = NDa
!p4 = NTotale
!p5 = NINLETTERE
!p6 = Ndescrizione
!p7 = Nnote
!p8 = NMetodo
End With
qdfRic.Execute dbFailOnError
....
With qdfprimanota
!p1 = NumRec
!p2 = NumConto
!p3 = Ndescrizione
!p4 = NData
If NEU = "E" Then
!p5 = NTotale
!p6 = Null
Else
!p5 = Null
!p6 = NTotale
End If
!p7 = Nnote
!p8 = NMoName
End With
qdfprimanota.Execute dbFailOnError
The Mysql backend refused to work in this way (error 3155, no other info, I am not sure why, but probably there is a reason, maybe someone may enlighten me), so I changed the code as follows
Code:
StrSql = "INSERT INTO Ricevute (Numero, Data, Da, Totale, INLETTERE, Descrizione, [Note], MetodoPagamento) " & _
"VALUES (" & NumRec & ",#" & NData & "#,'" & NDa & "'," & NTotale & ",'" & NINLETTERE & "','" & Ndescrizione & "','" & Nnote & "','" & NMetodo & "');"
dbsRicevute.Execute (StrSql)
....
StrSqlPN = "INSERT INTO PrimaNota (Ricevuta,[ContoN],Descrizione,Data,Entrate,Uscite,[Note],Mese) " & _
"VALUES (" & NumRec & "," & NumConto & ",'" & Ndescrizione & "',#" & NData & "#," & NTotale & "," & Null & ",'" & Nnote & "','" & NMoName & "');"
dbsRicevute.Execute (StrSqlPN)
Now inserting into the first table (first Execute) works, inserting into the second table fails complaining about syntax error in Insert into. I write here the two INSERT INTO statements (taken by debug.print just before the second execute command, the one that fails):I cannot spot the syntax error in the second string. What am I doing wrong?
INSERT INTO Ricevute (Numero, Data, Da, Totale, INLETTERE, Descrizione, [Note], MetodoPagamento) VALUES (5377,#18/01/2025#,'Antonino Abate (2061) (1717)',15,'quindici / 00','Quote associative di rinnovo',' - Antonino Abate (2061) (1717)','1');
INSERT INTO PrimaNota (Ricevuta,[ContoN],Descrizione,Data,Entrate,Uscite,[Note],Mese) VALUES (5377,10200,'Quote associative di rinnovo',#18/01/2025#,15,,' - Antonino Abate (2061) (1717)','GENNAIO');