1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| Option Compare Database
Option Explicit
'Requête par défaut (ôter le ;)
Private Const BASE_QUERY As String = "SELECT [Code client], Fonction, Société, Ville, [Code postal], Pays FROM Clients "
'Variable de module : garde sa valeur en permanence ici.
Private m_strOccurrence As String
Private Sub cmdReset_Click()
'Remise à zéro
m_strOccurrence = vbNullString
Me.lstCustomers.RowSource = BASE_QUERY
Me.txtFilter.SetFocus
Me.txtFilter.Value = vbNullString
Me.lblRecordCount.Caption = DCount("*", "Clients") & " clients trouvés"
End Sub
Private Sub Form_Load()
'Initialisation des contrôles et de la liste avec la requête par défaut
Me.fraFields.SetFocus
Me.fraFields = 4
Me.cmdReset.Enabled = False
Me.txtFilter.Enabled = False
Me.txtFilter.Value = vbNullString
lstCustomers.RowSource = BASE_QUERY
Me.lblRecordCount.Caption = DCount("*", "Clients") & " clients trouvés"
End Sub
Private Sub fraFields_AfterUpdate()
'Si ce n'est pas 'Tout' alors...
Me.cmdReset.Enabled = fraFields <> 4
Me.txtFilter.Enabled = fraFields <> 4
Me.txtFilter.Value = IIf(fraFields <> 4, txtFilter.Value, vbNullString)
If fraFields <> 4 Then Me.txtFilter.SetFocus
m_strOccurrence = vbNullString
End Sub
Private Sub txtFilter_KeyPress(KeyAscii As Integer)
'Gestion des critères avec MAJ dynamique de la liste...
Dim strSQLWhere As String
Dim strFieldname As String
Dim lngRecordCount As Long
Select Case KeyAscii
'Seuls ces caractère sont autorisés
Case 8, 32, 48 To 57, 65 To 90, 95, 97 To 122, 156, 192 To 221, 224 To 246, 249 To 253
Case Else
Beep
KeyAscii = 0
Exit Sub
End Select
Select Case fraFields
Case 1: strFieldname = "[Fonction]"
Case 2: strFieldname = "[Ville]"
Case 3: strFieldname = "[Pays]"
'Case ....
End Select
'Concaténation des cacactères
m_strOccurrence = m_strOccurrence & Chr(KeyAscii)
'Construction de la clause WHERE
strSQLWhere = strFieldname & " LIKE " & Chr(34) & m_strOccurrence & "*" & Chr(34)
'Vérification
lngRecordCount = DCount(strFieldname, "Clients", strSQLWhere)
'MAJ de la liste ou message selon le cas
If lngRecordCount Then
Me.lstCustomers.RowSource = BASE_QUERY & "WHERE " & strSQLWhere
Me.lblRecordCount.Caption = lngRecordCount & " client(s) trouvé(s)"
Else
MsgBox "Aucun enregistrement trouvé pour cette occurence !", vbExclamation
cmdReset_Click
End If
End Sub |
Partager