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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| Private Sub cmdRechercher_Click()
If Rs.state = adStateOpen Then Rst.Close: Set Rst = Nothing 'theoriquement cela aurai dût être fait en fin de passage précédant
If Cnx.state = adStateOpen Then Cnx.Close: Set Cnx = Nothing '??? pourquoi fermer la connexion à la BDs
'Recherche par rapport au Nom&Prénom
'On Error GoTo err '<-------- mauvaise habitude, mot reservé, pourquoi pas "GestErr"
On Error Resume Next
'Etablir la connexion <------------ dommage de faire une connexion à chaque recherche
Dim MsG As String, SQL As String
SQL = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\GCaisse.mdb;User Id=admin;Password=;"
Cnx.CursorLocation = adUseClient: Cnx.Mode = adModeRead
Cnx.Open SQL
If err.Number <> 0 Then
MsG = "Erreur N°" & err.Number & vbCrLf _
& "Description:" & vbCrLf & err.Description & vbCrLf _
& "Impossible d'ouvrire la BDs "
MsgBox MsG, vbCritical, "Erreur"
Exit Sub
End If
'Recherche par nom_Client
If OptBClient.Value = True Then
If CmbClient.Text = "Tous" Then
'Tous
SQL = "SELECT * FROM Caisse"
'Rst.Open "SELECT * FROM Caisse", Cnx, adOpenDynamic, adLockOptimistic, adCmdText
Else
'Selection que d'un client
SQL = "SELECT * FROM Caisse where Nom_Client='" & CmbClient.List(CmbClient.ListIndex) & "'"
'Rst.Open "SELECT * FROM Caisse where Nom_Client='" & CmbClient.List(CmbClient.ListIndex) & "'", Cnx, adOpenDynamic, adLockOptimistic, adCmdText
End If
End If
If OptBDate.Value = True Then
Dim DateDebut As Date, DateFin As Date
DateDebut = DateValue(Format(DTPicker1, "dd/mm/yyyy"))
DateFin = DateValue(Format(DTPicker2, "dd/mm/yyyy"))
If DateFin < DateDebut Then
MsgBox "Attention DateFin doit-être suppérieure à DateDébut", , "le Bon Voyqge)"
End If
' on inverse les champs mois et jour a cause de l'inversion SQL
DateDebut = Mid(DateDebut, 4, 3) & Left(DateDebut, 3) & Mid(DateDebut, 7)
DateFin = Mid(DateFin, 4, 3) & Left(DateFin, 3) & Mid(DateFin, 7)
'Rst.Open " SELECT * FROM Caisse WHERE [DateJournée] >= #" & Format(DateDebut, "dd/mm/yyyy") & " # AND [DateJournée] <= #" & Format(DateFin, "dd/mm/yyyy") & " # ", Cnx, adOpenDynamic, adLockPessimistic, adCmdText
SQL = "SELECT * FROM Caisse WHERE [DateJournée] >= #" & Format(DateDebut, "dd/mm/yyyy") & " # AND [DateJournée] <= #" & Format(DateFin, "dd/mm/yyyy") & "#"
End If
On Error Resume Next
Rst.Open SQL, Cnx, adOpenDynamic, adLockPessimistic, adCmdText
If err.Number <> 0 Then
MsG = "Erreur N°" & err.Number & vbCrLf _
& "Description:" & vbCrLf & err.Description & vbCrLf _
& "Impossible d'ouvrire la table Caisse"
MsgBox MsG, vbCritical, "Erreur"
Exit Sub
End If
If Rst.state = adStateOpen Then
If Rst.RecordCount >= 1 Then
ListView1.ListItems.Clear
'Mise a jour de la listview
For a = 0 To Rst.RecordCount - 1
Set itmX = ListView1.ListItems.Add(, , Rst.Fields(1))
itmX.SubItems(1) = Rst.Fields(2)
itmX.SubItems(2) = Rst.Fields(3)
itmX.SubItems(3) = Rst.Fields(4)
itmX.SubItems(4) = Rst.Fields(5)
itmX.SubItems(5) = Rst.Fields(6)
itmX.SubItems(6) = Rst.Fields(7)
itmX.SubItems(7) = Rst.Fields(8)
itmX.SubItems(8) = Rst.Fields(9)
Rst.MoveNext
Next a
'Rst.Close <----------------- ???? pourquoi ne pas fermer le recordset ici
Dim K As Integer
Dim Total As Currency
With ListView1
For K = 1 To .ListItems.Count
Total = Total + .ListItems(K).ListSubItems(4).Text
Next K
Tbox(9).Text = Format(Total, "# ##0.00")
End With
Else
MsgBox "Aucun enregistrement disponnible pour le critère de selection", vbInformation
End If
End If
'Exit Sub
'err:
' If err.Number = 3021 Then Exit Sub
End Sub |
Partager