[QUOTE]Originariamente inviato da Brainjar
codice:
Dim sSQL As String
Dim sWhere As String
Dim a As Integer
sSQL = "Select * from Tabella where"
sWhere = ""
For a = 0 To List1.ListCount - 1
If List1.Selected(a) = True Then
If IsEmpty(sWhere) Then
sWhere = " Cognome = '" & List1.List(a) & "'"
Else
sWhere = " AND Cognome = '" & List1.List(a) & "'"
End If
End If
Next a
sSQL = sSQL & sWhere
'
' In questo punto hai la stringa sSQL composta dinamicamente.
'
Ciao, ci sono alcuni piccoli errori (evidenziati in rosso), sicuramente dovuti a distrazione.. la versione corretta (con una piccola modifica):
codice:
Dim sSQL As String
Dim sWhere As String
Dim a As Integer
sSQL = "Select * from Tabella where"
sWhere = ""
For a = 0 To List1.ListCount - 1
If List1.Selected(a) = True Then
If IsEmpty(sWhere) Then
sWhere = " Cognome = '" & Replace(List1.List(a),"'","''") & "'"
Else
sWhere = sWhere & " OR Cognome = '" & Replace(List1.List(a),"'","''") & "'"
End If
End If
Next a
'meglio fare un controllo se sWhere contiene qualcosa, altrimenti non devi eseguire la query...
sSQL = sSQL & sWhere