o be fair, today I managed to find an error…I didn’t understand why the results were not what they should have been…then I realized that there was an error in the database design…in practice: Table NominativiDitte has an ID+Nome, the second table IntestazioneFatturaAcquisti has an ID and an Id referring to the ID of the first table, called ID_Ditta. The third table CorpoFatturaAcquisti has a column called ID_Intestazione that refers to the ID_Ditta of the second table. Doing so works, so I had to update the code like this: me.ListBoxRicerca.Visible=false
me.ListBoxRicerca.HasVerticalScrollbar=False
Try
’ Calcoliamo l’offset per la paginazione
Dim offset As Integer = currentPage * resultsPerPage
’ Query per il conteggio totale dei risultati
Dim countSQL As String = "SELECT COUNT(*) AS total FROM NominativiDitte " + _
"JOIN IntestazioneFatturaAcquisti ON NominativiDitte.ID = IntestazioneFatturaAcquisti.ID_DITTA " + _
“JOIN CorpoFatturaAcquisti ON IntestazioneFatturaAcquisti.ID = CorpoFatturaAcquisti.ID_Intestazione”
Dim rsCount As RecordSet = db.SQLSelect(countSQL)
If rsCount = Nil Then
MessageBox("Errore nell’eseguire la query di conteggio: " + db.ErrorMessage)
Return
End If
totalResults = rsCount.Field(“total”).IntegerValue
rsCount.Close
’ Costruzione della query di ricerca con alias
Dim sql As String
sql = "SELECT " + _
"NominativiDitte.ID AS DITTAID, " + _
"NominativiDitte.NomeDitta, " + _
"IntestazioneFatturaAcquisti.ID AS FATTURAID, " + _
"IntestazioneFatturaAcquisti.ID_DITTA, " + _
"IntestazioneFatturaAcquisti.NumeroFattura, " + _
"IntestazioneFatturaAcquisti.Data, " + _
"IntestazioneFatturaAcquisti.CostoSpedizioneOrdine, " + _
"CorpoFatturaAcquisti.ID as CORPOID, " + _
"CorpoFatturaAcquisti.ID_Intestazione, " + _
"CorpoFatturaAcquisti.Materiale_Spedito, " + _
"CorpoFatturaAcquisti.CostoKg, " + _
"CorpoFatturaAcquisti.CostoMetro, " + _
"CorpoFatturaAcquisti.CostoUnita, " + _
"CorpoFatturaAcquisti.QuantiKg, " + _
"CorpoFatturaAcquisti.QuantiMetri, " + _
"CorpoFatturaAcquisti.QuanteUnita " + _
"FROM NominativiDitte " + _
"JOIN IntestazioneFatturaAcquisti ON NominativiDitte.ID = IntestazioneFatturaAcquisti.ID_Ditta " + _
"JOIN CorpoFatturaAcquisti ON IntestazioneFatturaAcquisti.ID=CorpoFatturaAcquisti.ID_Intestazione " + _
"WHERE CorpoFatturaAcquisti.Materiale_Spedito LIKE ? " + _
"AND CorpoFatturaAcquisti.Materiale_Spedito LIKE ? "
’ Aggiungi il filtro per NomeDitta se è selezionato un valore nel PopupMenu
Dim sqlFiltroDitta As String = “”
If PopupMenu1.SelectedRowValue <> “” Then
sqlFiltroDitta = "AND NominativiDitte.NomeDitta = ? "
End If
sql = sql + sqlFiltroDitta + "LIMIT " + Str(resultsPerPage) + " OFFSET " + Str(offset)
’ Parametri per la query
Dim paramCampo1 As String = “%” + Campo1 + “%”
Dim paramCampo2 As String = “%” + Campo2 + “%”
’ Parametro per NomeDitta, solo se è selezionata una ditta nel PopupMenu
Dim paramNomeDitta As String = “”
If PopupMenu1.SelectedRowValue <> “” Then
paramNomeDitta = PopupMenu1.SelectedRowValue.ReplaceAll(“'”, “‘’”)
End If
’ Esegui la query con i parametri
If paramNomeDitta <> “” Then
rows2 = db.SelectSQL(sql, paramCampo1, paramCampo2, paramNomeDitta)
Else
rows2 = db.SelectSQL(sql, paramCampo1, paramCampo2)
End If
If rows2 = Nil Then
MessageBox("Errore nell’eseguire la query di ricerca: " + db.ErrorMessage)
Return
End If
’ Pulisce la ListBox prima di aggiungere nuovi dati
ListBoxRicerca.RemoveAllRows
’ Aggiungi le righe alla ListBox
While Not rows2.AfterLastRow
ListBoxRicerca.AddRow(rows2.column(“DITTAID”).StringValue, _
rows2.column(“CORPOID”).StringValue, _
rows2.column(“NomeDitta”).StringValue, _
rows2.column(“Data”).StringValue, _
rows2.column(“NumeroFattura”).StringValue, _
rows2.column(“Materiale_Spedito”).StringValue)
rows2.MoveToNextRow
Wend
’ Abilita/disabilita i pulsanti in base alla pagina corrente
ButtonIndietro.Enabled = (currentPage > 0)
ButtonAvanti.Enabled = (currentPage + 1) * resultsPerPage < totalResults
Catch error As DatabaseException
MessageBox("Error: " + error.Message)
End Try
me.ListBoxRicerca.Visible=True
me.ListBoxRicerca.HasVerticalScrollbar=True