Fichier pdf

Transcription

Fichier pdf
Corrigé
Tutoriel 10
Leçon A
Exercices :
1.
a. Dim strMois(1 to 6) as String
b. StrMois(1) = "Janvier"
StrMois(2) = "Février"
StrMois(3) = "Mars"
StrMois(4) = "Avril"
StrMois(5) = "Mai"
StrMois(6) = "Juin"
c. Janvier
Février
Mars
Avril
Mai
Juin
2.
StrMois(1)
StrMois(2)
StrMois(3)
StrMois(4)
StrMois(5)
StrMois(6)
a. Dim intResultat(1 to 10) as Integer
b. Dim intX as Integer
Open "a:\tut10\resultat.dat" for input As #1
For intX = 1 to 10
Input #1, intResultat(intX)
Next intX
Close #1
3.
a. Dim strEmploye(1 to 3, 1 to 2) as String
b. Dim intLigne as Integer, intCol as Integer
Open "a:\tut10\employ.dat" for input As #1
For intLigne = 1 to 3
For intCol = 1 to 2
Input #1, strEmploye(intLigne, intCol)
Next intCol
Next intLigne
Close #1
1
c. Dim intLigne as Integer, intCol as Integer
Open "a:\tut10\employ.dat" for input As #1
For intCol = 1 to 2
For intLigne = 1 to 3
Input #1, strEmploye(intLigne, intCol)
Next intLigne
Next intCol
Close #1
4. la4Fait
la4Fait.frm
Option Explicit
Dim strCapitales(1 To 50) As String
Private Sub cmdQuitter_Click()
Unload frmEtats
End Sub
Private Sub cmdImprimer_Click()
PrintForm
End Sub
Private Sub Form_Load()
Dim strEtats As String, strCap As String, intX As Integer
frmEtats.Left = (Screen.Width - frmEtats.Width) / 2
frmEtats.Top = (Screen.Height - frmEtats.Height) / 2
Open "A:\Tut10\capital.dat" For Input As #1
Do While Not EOF(1)
Input #1, strEtats, strCap
intX = intX + 1
lstEtat.AddItem strEtats
strCapitales(intX) = strCap
2
Loop
Close #1
lstEtat.ListIndex = 0
End Sub
Private Sub lstEtat_Click()
lblCapital.Caption = strCapitales(lstEtat.ListIndex + 1)
End Sub
5. la5Fait
la5Fait.frm
Option Explicit
Dim strEmploy(1 To 20, 1 To 2) As String
Private Sub cmdQuitter_Click()
Unload frmEmploy
End Sub
Private Sub cmdImprimer_Click()
PrintForm
End Sub
Private Sub cmdRech_Click()
Dim intLigne As Integer, intUsager As Integer, strNum As String, strTrouv As String
Const conBtns As String = _
vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
strNum = txtNum.Text
strTrouv = "N"
Do While intLigne < 20 And strTrouv = "N"
intLigne = intLigne + 1
If Trim(strEmploy(intLigne, 1)) = Trim(strNum) Then
lblNom.Caption = strEmploy(intLigne, 2)
strTrouv = "O"
3
End If
Loop
If strTrouv = "N" Then
intUsager = MsgBox("Numéro d'employé incorrect.", conBtns, "Erreur de numéro")
End If
txtNum.SetFocus
End Sub
Private Sub Form_Load()
Dim intLigne As Integer
frmEmploy.Left = (Screen.Width - frmEmploy.Width) / 2
frmEmploy.Top = (Screen.Height - frmEmploy.Height) / 2
Open "A:\Tut10\employ.dat" For Input As #1
For intLigne = 1 To 20
Input #1, strEmploy(intLigne, 1), strEmploy(intLigne, 2)
Next intLigne
Close #1
End Sub
Private Sub txtNum_GotFocus()
txtNum.SelStart = 0
txtNum.SelLength = Len(txtNum.Text)
End Sub
6. la6Fait
la6Fait.frm
Option Explicit
Dim intNum(1 To 20) As Integer
Private Sub cmdQuitter_Click()
Unload frmPoints
End Sub
4
Private Sub cmdImprimer_Click()
PrintForm
End Sub
Private Sub cmdRech_Click()
Dim intMin As Integer, intEtud As Integer, intX As Integer
intMin = intNum(1)
intEtud = 1
For intX = 2 To 20
If intNum(intX) < intMin Then
intMin = intNum(intX)
intEtud = 1
Else
If intNum(intX) = intMin Then
intEtud = intEtud + 1
End If
End If
Next intX
lblMinimum.Caption = intMin
lblNbEtud.Caption = intEtud
End Sub
Private Sub Form_Load()
Dim intX As Integer
frmPoints.Top = (Screen.Height - frmPoints.Height) / 2
frmPoints.Left = (Screen.Width - frmPoints.Width) / 2
Open "A:\Tut10\points.dat" For Input As #1
For intX = 1 To 20
Input #1, intNum(intX)
Next intX
Close #1
End Sub
7. la7Fait
5
la7Fait.frm
Option Explicit
Dim intPoints(1 To 5) As Integer, strNotes(1 To 5) As String
Private Sub cmdAff_Click()
Dim intX As Integer, intNum As Integer, strTrouv As String, intUsager As Integer
Const conBtns As String = _
vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
intNum = Val(txtPoints.Text)
If intNum >= 0 Then
strTrouv = "N"
intX = 5
Do While intX > 0 And strTrouv = "N"
If intPoints(intX) <= intNum Then
lblNote.Caption = strNotes(intX)
strTrouv = "O"
Else
intX = intX - 1
End If
Loop
Else
intUsager = MsgBox("Nombre de points incorrect .", conBtns, "Erreur d'entrée")
End If
txtPoints.SetFocus
End Sub
Private Sub cmdQuitter_Click()
Unload frmNote
End Sub
Private Sub cmdImprimer_Click()
PrintForm
End Sub
Private Sub Form_Load()
frmNote.Top = (Screen.Height - frmNote.Height) / 2
frmNote.Left = (Screen.Width - frmNote.Width) / 2
intPoints(1) = 0
intPoints(2) = 300
intPoints(3) = 350
intPoints(4) = 400
intPoints(5) = 450
strNotes(1) = "E"
strNotes(2) = "D"
strNotes(3) = "C"
strNotes(4) = "B"
strNotes(5) = "A"
End Sub
6
Private Sub txtPoints_Change()
lblNote.Caption = ""
End Sub
Private Sub txtPoints_GotFocus()
txtPoints.SelStart = 0
txtPoints.SelLength = Len(txtPoints.Text)
End Sub
8. la8Fait
la8Fait.frm
Option Explicit
Dim intPoints(1 To 5) As Integer, strNotes(1 To 5) As String
Private Sub cmdAfficher_Click()
Dim intX As Integer, intNum As Integer, strTrouv As String, intUsager As Integer
Const conBtns As String = _
vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
intNum = Val(txtPoints.Text)
If intNum >= 0 Then
strTrouv = "N"
intX = 1
Do While intX <= 5 And strTrouv = "N"
If intPoints(intX) >= intNum Then
lblNote.Caption = strNotes(intX)
strTrouv = "O"
Else
intX = intX + 1
End If
Loop
Else
intUsager = MsgBox("Note incorrecte.", conBtns, "Erreur d'entrée")
End If
7
txtPoints.SetFocus
End Sub
Private Sub cmdQuitter_Click()
Unload frmNote
End Sub
Private Sub cmdImprimer_Click()
PrintForm
End Sub
Private Sub Form_Load()
frmNote.Top = (Screen.Height - frmNote.Height) / 2
frmNote.Left = (Screen.Width - frmNote.Width) / 2
intPoints(1) = 299
intPoints(2) = 349
intPoints(3) = 399
intPoints(4) = 449
intPoints(5) = 500
strNotes(1) = "E"
strNotes(2) = "D"
strNotes(3) = "C"
strNotes(4) = "B"
strNotes(5) = "A"
End Sub
Private Sub txtPoints_Change()
lblNote.Caption = ""
End Sub
Private Sub txtPoints_GotFocus()
txtPoints.SelStart = 0
txtPoints.SelLength = Len(txtPoints.Text)
End Sub
9. la9Fait
8
la9Fait.frm
Option Explicit
Dim intPoints(1 To 5) As Integer, strNotes(1 To 5) As String
Private Sub cmdAff_Click()
Dim intX As Integer, intNum As Integer, strTrouv As String, intUsager As Integer
Const conBtns As String = _
vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
intNum = Val(txtPoints.Text)
If intNum >= 0 Then
strTrouv = "N"
intX = 5
Do While intX > 0 And strTrouv = "N"
If intPoints(intX) <= intNum Then
lblNote.Caption = strNotes(intX)
strTrouv = "O"
Else
intX = intX - 1
End If
Loop
Else
intUsager = MsgBox("Nombre de points incorrect .", conBtns, "Erreur d'entrée")
End If
txtPoints.SetFocus
End Sub
Private Sub cmdQuitter_Click()
Unload frmNote
End Sub
Private Sub cmdImprimer_Click()
PrintForm
End Sub
Private Sub Form_Load()
Dim intMaxPoints As Integer
frmNote.Top = (Screen.Height - frmNote.Height) / 2
frmNote.Left = (Screen.Width - frmNote.Width) / 2
intMaxPoints = InputBox("Entrez la note maximale.", "input")
intPoints(1) = 0
intPoints(2) = 0.6 * intMaxPoints
intPoints(3) = 0.7 * intMaxPoints
intPoints(4) = 0.8 * intMaxPoints
intPoints(5) = 0.9 * intMaxPoints
strNotes(1) = "E"
strNotes(2) = "D"
strNotes(3) = "C"
strNotes(4) = "B"
9
strNotes(5) = "A"
End Sub
Private Sub txtPoints_Change()
lblNote.Caption = ""
End Sub
Private Sub txtPoints_GotFocus()
txtPoints.SelStart = 0
txtPoints.SelLength = Len(txtPoints.Text)
End Sub
10. (Découverte) la10Fait
la10Fait.frm
Option Explicit
Dim udtNotes(1 To 5) As Etudiant
Private Sub cmdAff_Click()
Dim intX As Integer, intNum As Integer, strTrouv As String, intUsager As Integer
Const conBtns As String = _
vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
intNum = Val(txtPoints.Text)
If intNum >= 0 Then
strTrouv = "N"
intX = 5
Do While intX > 0 And strTrouv = "N"
If udtNotes(intX).intPoints <= intNum Then
lblNote.Caption = udtNotes(intX).strNotes
strTrouv = "O"
Else
intX = intX - 1
End If
Loop
10
Else
intUsager = MsgBox("Nombre de points incorrect .", conBtns, "Erreur d'entrée")
End If
txtPoints.SetFocus
End Sub
Private Sub cmdQuitter_Click()
Unload frmNote
End Sub
Private Sub cmdImprimer_Click()
PrintForm
End Sub
Private Sub Form_Load()
frmNote.Top = (Screen.Height - frmNote.Height) / 2
frmNote.Left = (Screen.Width - frmNote.Width) / 2
udtNotes(1).intPoints = 0
udtNotes(1).strNotes = "E"
udtNotes(2).intPoints = 300
udtNotes(2).strNotes = "D"
udtNotes(3).intPoints = 350
udtNotes(3).strNotes = "C"
udtNotes(4).intPoints = 400
udtNotes(4).strNotes = "B"
udtNotes(5).intPoints = 450
udtNotes(5).strNotes = "A"
End Sub
Private Sub txtPoints_Change()
lblNote.Caption = ""
End Sub
Private Sub txtPoints_GotFocus()
txtPoints.SelStart = 0
txtPoints.SelLength = Len(txtPoints.Text)
End Sub
la10Fait.bas
Option Explicit
Type Etudiant
intPoints As Integer
strNotes As String
End Type
11
11. (Découverte) la11Fait
a. Test11.dat
85
87
70
76
100
30
78
88
95
92
29
88
65
47
20
99
100
32
73
72
j. Test11.dat
90
92
75
81
105
35
83
93
100
97
34
93
70
52
25
104
105
37
78
77
a. Test11X.dat
85
87
70
76
93
20
j. Test11X.dat
95
97
80
86
103
30
la11Fait.frm
Option Explicit
Dim intOrdEnr() As Integer, intComptEnr As Integer
Private Sub cmdMoy_Click()
Dim intNum As Integer, intTotal As Integer, sngMoy As Single
For intNum = 1 To intComptEnr
intTotal = intTotal + intOrdEnr(intNum) 'accumuler les résultats du tableau
Next intNum
If intComptEnr > 0 Then
sngMoy = intTotal / intComptEnr
'calculer la moyenne
Else
sngMoy = 0
End If
lblMsg.Caption = "Moyenne"
'afficher les résultats
12
lblReponse.Caption = Format(sngMoy, "standard")
End Sub
Private Sub cmdQuitter_Click()
Unload frmTest
End Sub
Private Sub cmdMax_Click()
Dim intNum As Integer, intMax As Integer
intMax = intOrdEnr(1)
'initialiser intMax avec le premier élément du tableau
For intNum = 2 To intComptEnr
'comparer intMax aux éléments restants
If intOrdEnr(intNum) > intMax Then 'l'élément est plus grand que intMax
intMax = intOrdEnr(intNum)
End If
Next intNum
lblMsg.Caption = "Maximum"
'afficher la réponse
lblReponse.Caption = intMax
End Sub
Private Sub cmdMajour_Click()
Dim intNum As Integer, intAugm As Integer
intAugm = Val(InputBox("Entrer le nombre de points supplémentaires :"))
For intNum = 1 To intComptEnr
'ajouter les points supplémentaires à chaque résultat du
tableau
intOrdEnr(intNum) = intOrdEnr(intNum) + intAugm
Next intNum
lblMsg.Caption = "Résultats adaptés de " 'afficher le message
lblReponse.Caption = intAugm
Open "A:\Tut10\Test11x.dat" For Output As #1 'ouvrir le fichier
For intNum = 1 To intComptEnr
'écrire le contenu du tableau dans le fichier
Write #1, intOrdEnr(intNum)
Next intNum
Close #1
'fermer le fichier
End Sub
Private Sub Form_Load()
frmTest.Left = (Screen.Width - frmTest.Width) / 2
frmTest.Top = (Screen.Height - frmTest.Height) / 2
Dim intNum As Integer
Open "A:\Tut10\test11x.dat" For Input As #1 'ouvrir le fichier
Do While Not EOF(1)
'remplir le tableau avec les données
intComptEnr = intComptEnr + 1
ReDim Preserve intOrdEnr(intComptEnr)
Input #1, intOrdEnr(intComptEnr)
Loop
Close #1
End Sub
13
12. (Découverte) la12Fait
a. ventesFB.dat
10000
5000
40000
46000
25000
8400
7500
3450
35000
57000
23000
5555
1111
2222
45002
46701
2300
500
6787
2345
e. ventesFB.dat
11000
5500
44000
50600
27500
9240
8250
3795
38500
62700
25300
6110.5
1222.1
2444.2
49502.2
51371.1
2530
550
7465.7
2579.5
la12Fait.frm
Option Explicit
Dim sngVentes(1 To 10, 1 To 2) As Single
Private Sub cmdMax_Click()
Dim intLigne As Integer, intCol As Integer, sngMax As Single
sngMax = sngVentes(1, 1)
'initialiser sngMax avec le premier élément du tableau
For intLigne = 2 To 10
'comparer sngMax aux éléments restants
For intCol = 1 To 2
If sngVentes(intLigne, intCol) > sngMax Then 'l'élément est plus grand que sngMax
sngMax = sngVentes(intLigne, intCol)
End If
Next intCol
Next intLigne
lblMsg.Caption = "Maximum"
'afficher la réponse
lblReponse.Caption = Format(sngMax, "standard")
End Sub
Private Sub cmdMoy_Click()
Dim intLigne As Integer, intCol As Integer, sngTotal As Single, sngMoy As Single
For intLigne = 1 To 10
For intCol = 1 To 2
14
sngTotal = sngTotal + sngVentes(intLigne, intCol) 'accumuler les résultats du tableau
Next intCol
Next intLigne
sngMoy = sngTotal / 20
'calculer la moyenne
lblMsg.Caption = "Moyenne"
'afficher les résultats
lblReponse.Caption = Format(sngMoy, "standard")
End Sub
Private Sub cmdQuitter_Click()
Unload frmTest
End Sub
Private Sub cmdMin_Click()
Dim intLigne As Integer, intCol As Integer, sngMin As Single
sngMin = sngVentes(1, 1)
'initialiser sngMin avec le premier élément du tableau
For intLigne = 2 To 10
'comparer sngMin aux éléments restants
For intCol = 1 To 2
If sngVentes(intLigne, intCol) < sngMin Then 'l'élément est plus grand que sngMin
sngMin = sngVentes(intLigne, intCol)
End If
Next intCol
Next intLigne
lblMsg.Caption = "Minimum"
'afficher la réponse
lblReponse.Caption = Format(sngMin, "standard")
End Sub
Private Sub cmdMajour_Click()
Dim intLigne As Integer, intCol As Integer, sngAugm As Single
sngAugm = Val(InputBox("Entrer l'augmentation :"))
For intLigne = 1 To 10
'ajouter les points supplémentaires à chaque résultat du tableau
For intCol = 1 To 2
sngVentes(intLigne, intCol) = sngVentes(intLigne, intCol) * (1 + sngAugm)
Next intCol
Next intLigne
lblMsg.Caption = "Ventes augmentées de " 'afficher le message
lblReponse.Caption = Format(sngAugm, "percent")
Open "A:\Tut10\ventesFB.dat" For Output As #1 'ouvrir le fichier
For intLigne = 1 To 10
'écrire le contenu du tableau dans le fichier
For intCol = 1 To 2
Write #1, sngVentes(intLigne, intCol)
Next intCol
Next intLigne
Close #1
'fermer le fichier
End Sub
Private Sub Form_Load()
frmTest.Left = (Screen.Width - frmTest.Width) / 2
frmTest.Top = (Screen.Height - frmTest.Height) / 2
Dim intLigne As Integer, intCol As Integer
15
Open "A:\Tut10\ventesFB.dat" For Input As #1 'ouvrir le fichier
For intLigne = 1 To 10
'remplir le tableau avec les données
For intCol = 1 To 2
Input #1, sngVentes(intLigne, intCol)
Next intCol
Next intLigne
Close #1
End Sub
13. (Découverte) la13Fait
derome.dat
10000,5000,4000
40000,46000,10000
25000,8400,25000
7500,3450,15000
35000,57000,25000
23000,5555,7500
1111,2222,5000
45002,46701,45000
2300,500,1500
6787,2345,3000
la13Fait.frm
Option Explicit
Dim sngVentes(1 To 10, 1 To 3) As Single
Private Sub cmdImprimer_Click()
Dim intLigne As Integer, intCol As Integer, sngTaux As Single
Dim sngVendeurTot As Single, sngComm As Single, sngTotalComm As Single
Dim strPolice As String, sngSize As Single
Dim strPS1 As String * 12, strPS2 As String * 10
sngTaux = Val(txtComm.Text)
strPolice = Printer.Font
'sauvegarde de la configuration actuelle de l'imprimante
sngSize = Printer.FontSize
Printer.Font = "courier new" 'changement de la configuration
Printer.FontSize = 12
'Impression des titres et en-têtes
Printer.Print Tab(30); "Loisirs De Rome"
Printer.Print
Printer.Print Tab(5); "Pourcentage de commission : " & Format(sngTaux, "percent")
Printer.Print
Printer.Print
Printer.Print Tab(5); "Numéro"; Tab(18); "Ventes"; Tab(33); "Bonus"
16
Printer.Print Tab(5); "------"; Tab(15); "------------"; Tab(30); "------------"
For intLigne = 1 To 10
For intCol = 1 To 3
sngVendeurTot = sngVendeurTot + sngVentes(intLigne, intCol)
Next intCol
sngComm = sngVendeurTot * sngTaux
RSet strPS1 = Format(sngVendeurTot, "standard")
RSet strPS2 = Format(sngComm, "standard")
Printer.Print Tab(5); intLigne; Tab(15); strPS1; Tab(30); strPS2
sngTotalComm = sngTotalComm + sngComm
sngVendeurTot = 0
Next intLigne
Printer.Print
'Impression de 2 lignes blanches
Printer.Print
'Impression du grand total
RSet strPS2 = Format(sngTotalComm, "currency")
Printer.Print Tab(5); "Total des commissions payées :"; Tab(30); strPS2
Printer.Print
'Impression d'une ligne blanche
Printer.Print Tab(5); "Fin de rapport" 'Impression du message
Printer.EndDoc
'Envoie du rapport vers l'imprimante
Printer.Font = strPolice
Printer.FontSize = sngSize
End Sub
Private Sub cmdQuitter_Click()
Unload frmDerome
End Sub
Private Sub Form_Load()
Dim intLigne As Integer, intCol As Integer
frmDerome.Left = (Screen.Width - frmDerome.Width) / 2
frmDerome.Top = (Screen.Height - frmDerome.Height) / 2
Open "A:\Tut10\derome.dat" For Input As #1
For intLigne = 1 To 10
For intCol = 1 To 3
Input #1, sngVentes(intLigne, intCol)
Next intCol
Next intLigne
End Sub
Private Sub txtComm_GotFocus()
txtComm.SelStart = 0
txtComm.SelLength = Len(txtComm.Text)
End Sub
17
14. (Découverte) la14Fait
a. test14.dat
85
87
70
76
100
30
78
88
95
92
29
88
65
47
20
99
100
32
73
72
e. test14.dat
85
87
75
76
105
30
78
88
95
92
29
88
65
47
20
99
100
32
73
72
la14Fait.frm
Private Sub cmdMajour_Click()
Dim strNum As String, intNum As Integer, intAugm As Integer
Dim intUsager As Integer, intX As Integer
Const conBtns As String = _
vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
'Saisie du numéro de l'étudiant ou de la lettre A
strNum = InputBox("Entrez le numéro de l'étudiant (entrez A pour mettre à jour tous les
étudiants):", "Étudiant")
intNum = Val(strNum)
strNum = UCase(strNum)
If strNum <> "A" And intNum < 1 Or intNum > 20 Then 'Entrée invalide
intUsager = MsgBox("Numéro d'étudiant erroné", conBtns, "Erreur d'entrée")
Else
'Saisie de l'augmentation
intAugm = Val(InputBox("Entrez le nombre de points supplémentaires :", "Points"))
If strNum = "A" Then
'ajouter des points à tous les nombres dans le tableau
For intX = 1 To 20
'ajouter les points supplémentaires à chaque résultat du tableau
intTabResultat(intX) = intTabResultat(intX) + intAugm
Next intX
Else
'Mise à jour individuel des points des étudiants
intTabResultat(intNum) = intTabResultat(intNum) + intAugm
18
End If
lblMsg.Caption = "Résultats augmentés de " 'afficher le message
lblReponse.Caption = intAugm
Open "A:\Tut10\Test14.dat" For Output As #1 'ouvrir le fichier
For intX = 1 To 20
'écrire le contenu du tableau dans le fichier
Write #1, intTabResultat(intX)
Next intX
Close #1
'fermer le fichier
End If
End Sub
15. (Découverte) la15Fait
Option Explicit
Dim intCompteur(1 To 3, 1 To 4) As Integer
Private Sub cmdAfficher_Click()
Dim strParti As String, intAge As Integer, intLigne As Integer, intCol As Integer
For intLigne = 1 To 3 'initialisation du tableau
For intCol = 1 To 4
intCompteur(intLigne, intCol) = 0
Next intCol
Next intLigne
Open "A:\Tut10\pao.dat" For Input As #1 'Ouverture d'un fichier séquentiel
Do While Not EOF(1)
Input #1, strParti, intAge
'lecture d'un enregistrement
Select Case strParti
'mise à jour de intCompteur
Case "Démocrate"
intCompteur(1, intAge + 1) = intCompteur(1, intAge + 1) + 1
Case "Républicain"
intCompteur(2, intAge + 1) = intCompteur(2, intAge + 1) + 1
Case "Indépendant"
intCompteur(3, intAge + 1) = intCompteur(3, intAge + 1) + 1
End Select
Loop
Close #1
'Fermeture du fichier
For intCol = 0 To 3
'Afficher le contrôle étiquette
lblDem(intCol).Caption = intCompteur(1, intCol + 1)
lblRep(intCol).Caption = intCompteur(2, intCol + 1)
lblInd(intCol).Caption = intCompteur(3, intCol + 1)
Next intCol
End Sub
Private Sub cmdEnregistrer_Click()
Open "A:\Tut10\pao.dat" For Append As #1 'Ouverture du fichier séquentiel
19
Write #1, lstParti.Text, lstAge.ListIndex 'Écriture de l'enregistrement
Close #1
'Fermeture du fichier
End Sub
Private Sub cmdQuitter_Click()
Unload frmPao
End Sub
Private Sub cmdImprimer_Click()
Dim intCol As Integer, intLigne As Integer, intDem As Integer, intRep As Integer
Dim intInd As Integer, intTotal As Integer
Dim strPolice As String, sngTaille As Single
Dim strCI1 As String * 3, strCI2 As String * 3, strCI3 As String * 3
Dim strCI4 As String * 3, strCI5 As String * 4
'accumuler les résultats
For intCol = 1 To 4
intDem = intDem + intCompteur(1, intCol)
intRep = intRep + intCompteur(2, intCol)
intInd = intInd + intCompteur(3, intCol)
Next intCol
intTotal = intDem + intRep + intInd
strPolice = Printer.Font
'enregistrer les paramètres d'impression
sngTaille = Printer.FontSize
Printer.Font = "courier new" 'modifier les paramètres d'impression
Printer.FontSize = 10
'imprimer le titre et les en-têtes
Printer.Print Tab(30); "Résultats PAO - 1999"
Printer.Print
Printer.Print Tab(5); "Parti"; Tab(20); "18-35"; Tab(30); "36-50"; _
Tab(40); "51-65"; Tab(50); "Plus de 65"; Tab(60); "Total"
'aligner les résultats Démocrates et imprimer
RSet strCI1 = Format(intCompteur(1, 1), "general number")
RSet strCI2 = Format(intCompteur(1, 2), "general number")
RSet strCI3 = Format(intCompteur(1, 3), "general number")
RSet strCI4 = Format(intCompteur(1, 4), "general number")
RSet strCI5 = Format(intDem, "general number")
Printer.Print Tab(5); "Démocrate"; Tab(22); strCI1; Tab(32); strCI2; _
Tab(42); strCI3; Tab(54); strCI4; Tab(61); strCI5
'aligner les résultats Républicains et imprimer
RSet strCI1 = Format(intCompteur(2, 1), "general number")
RSet strCI2 = Format(intCompteur(2, 2), "general number")
RSet strCI3 = Format(intCompteur(2, 3), "general number")
RSet strCI4 = Format(intCompteur(2, 4), "general number")
RSet strCI5 = Format(intRep, "general number")
Printer.Print Tab(5); "Républicain"; Tab(22); strCI1; Tab(32); strCI2; _
Tab(42); strCI3; Tab(54); strCI4; Tab(61); strCI5
'aligner les résultats Indépendants et imprimer
20
RSet strCI1 = Format(intCompteur(3, 1), "general number")
RSet strCI2 = Format(intCompteur(3, 2), "general number")
RSet strCI3 = Format(intCompteur(3, 3), "general number")
RSet strCI4 = Format(intCompteur(3, 4), "general number")
RSet strCI5 = Format(intInd, "general number")
Printer.Print Tab(5); "Indépendant"; Tab(22); strCI1; Tab(32); strCI2; _
Tab(42); strCI3; Tab(54); strCI4; Tab(61); strCI5
Printer.Print
'imprimer deux lignes blanches
Printer.Print
'imprimer le total
RSet strCI5 = Format(intTotal, "general number")
Printer.Print Tab(41); "Total des réponses"; Tab(61); strCI5
Printer.Print
'imprimer une ligne blanche
Printer.Print Tab(5); "Fin du rapport" 'imprimer le message
Printer.EndDoc
'envoyer le rapport à l'imprimante
Printer.Font = strPolice
Printer.FontSize = sngTaille
End Sub
Private Sub Form_Load()
frmPao.Top = (Screen.Height - frmPao.Height) / 2
frmPao.Left = (Screen.Width - frmPao.Width) / 2
lstAge.AddItem "18 - 35"
lstAge.AddItem "36 - 50"
lstAge.AddItem "51 - 65"
lstAge.AddItem "Plus de 65"
lstParti.AddItem "Républicain"
lstParti.AddItem "Démocrate"
lstParti.AddItem "Indépendant"
lstParti.ListIndex = 0
lstAge.ListIndex = 1
End Sub
21
Leçon B
Exercices :
1. lb1Fait
g)
h) q
j)
g)
Option Explicit
Private Sub dgdVentes_AfterColUpdate(ByVal ColIndex As Integer)
adoVentes.Recordset.Fields("fldCommission") =
Round(adoVentes.Recordset.Fields("fldVentes") * 0.1, 2)
End Sub
Private Sub Form_Load()
22
frmVentes.Left = (Screen.Width - frmVentes.Width) / 2
frmVentes.Top = (Screen.Height - frmVentes.Height) / 2
End Sub
Private Sub MenuFichierImprimer_Click()
PrintForm
End Sub
Private Sub MenuFichierQuitter_Click()
Unload frmVentes
End Sub
h)
Option Explicit
Private Sub dgdVentes_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object
Set objChamps = adoVentes.Recordset.Fields
objChamps("fldCommission") = Round(objChamps("fldVentes") * 0.1, 2)
End Sub
Private Sub Form_Load()
frmVentes.Left = (Screen.Width - frmVentes.Width) / 2
frmVentes.Top = (Screen.Height - frmVentes.Height) / 2
End Sub
Private Sub MenuFichierImprimer_Click()
PrintForm
End Sub
Private Sub MenuFichierQuitter_Click()
Unload frmVentes
End Sub
j)
Option Explicit
Private Sub dgdVentes_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Recordset
Set objChamps = adoVentes.Recordset
objChamps("fldCommission") = Round(objChamps.Fields("fldVentes") * 0.1, 2)
End Sub
Private Sub Form_Load()
23
frmVentes.Left = (Screen.Width - frmVentes.Width) / 2
frmVentes.Top = (Screen.Height - frmVentes.Height) / 2
End Sub
Private Sub MenuFichierImprimer_Click()
PrintForm
End Sub
Private Sub MenuFichierQuitter_Click()
Unload frmVentes
End Sub
2. lb2Fait
g)
h)
i)
g)
24
Option Explicit
Private Sub dgdPrix_AfterColUpdate(ByVal ColIndex As Integer)
adoPrix.Recordset.Fields("fldNouvPrix") = _
Round(adoPrix.Recordset.Fields("fldPrixAct") * _
(1 + adoPrix.Recordset.Fields("fldPourcent")), 2)
End Sub
Private Sub Form_Load()
frmPrix.Left = (Screen.Width - frmPrix.Width) / 2
frmPrix.Top = (Screen.Height - frmPrix.Height) / 2
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmPrix
End Sub
h)
Option Explicit
Private Sub dgdPrix_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object
Set objChamps = adoPrix.Recordset.Fields
objChamps("fldNouvPrix") = Round(objChamps("fldPrixAct") * _
(1 + objChamps("fldPourcent")), 2)
End Sub
Private Sub Form_Load()
frmPrix.Left = (Screen.Width - frmPrix.Width) / 2
frmPrix.Top = (Screen.Height - frmPrix.Height) / 2
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmPrix
End Sub
i)
25
Option Explicit
Private Sub dgdPrix_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Recordset
Set objChamps = adoPrix.Recordset
objChamps.Fields("fldNouvPrix") = Round(objChamps.Fields("fldPrixAct") * _
(1 + objChamps.Fields("fldPourcent")), 2)
End Sub
Private Sub Form_Load()
frmPrix.Left = (Screen.Width - frmPrix.Width) / 2
frmPrix.Top = (Screen.Height - frmPrix.Height) / 2
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmPrix
End Sub
3. lb3Fait
lb3Fait.frm
Option Explicit
Private Sub dgdFact_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object
Set objChamps = adoFact.Recordset.Fields
26
objChamps("fldTotal") = objChamps("fldElec") + _
objChamps("fldGaz") + objChamps("fldEau")
End Sub
Private Sub Form_Load()
frmFact.Left = (Screen.Width - frmFact.Width) / 2
frmFact.Top = (Screen.Height - frmFact.Height) / 2
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmFact
End Sub
4. lb4Fait
lb4Fait.frm
Option Explicit
Dim strTitres(1 To 4) As String
Private Sub dgdEmp_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object, intCode As Integer
Set objChamps = adoEmp.Recordset.Fields
intCode = Val(objChamps("fldCode"))
If intCode >= 1 And intCode <= 4 Then
objChamps("fldTitre") = strTitres(intCode)
Else
objChamps("fldTitre") = "Inconnu"
End If
27
End Sub
Private Sub Form_Load()
frmEmp.Left = (Screen.Width - frmEmp.Width) / 2
frmEmp.Top = (Screen.Height - frmEmp.Height) / 2
strTitres(1) = "Directeur"
strTitres(2) = "Secretaire"
strTitres(3) = "Contremaître"
strTitres(4) = "Ouvrier"
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmEmp
End Sub
5. lb5Fait
lb5Fait.frm
Option Explicit
Dim strTitres(1 To 4, 1 To 2) As String
Private Sub dgdEmp_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object, strCode As String
Dim intX As Integer, strTrouve As String
Set objChamps = adoEmp.Recordset.Fields
strCode = UCase(objChamps("fldCode"))
28
intX = 1
'recherche dans la première colonne du tableau
strTrouve = "N"
Do While intX <= 4 And strTrouve = "N"
If strTitres(intX, 1) = strCode Then
objChamps("fldTitre") = strTitres(intX, 2)
strTrouve = "O"
Else
intX = intX + 1
End If
Loop
If strTrouve = "N" Then 'si le code n'apparaît pas dans le tableau
objChamps("fldTitre") = "Inconnu"
End If
End Sub
Private Sub Form_Load()
frmEmp.Left = (Screen.Width - frmEmp.Width) / 2
frmEmp.Top = (Screen.Height - frmEmp.Height) / 2
strTitres(1, 1) = "A"
strTitres(1, 2) = "Directeur"
strTitres(2, 1) = "B"
strTitres(2, 2) = "Secretaire"
strTitres(3, 1) = "C"
strTitres(3, 2) = "Contremaître"
strTitres(4, 1) = "D"
strTitres(4, 2) = "Ouvrier"
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmEmp
End Sub
6. (Découverte) lb6Fait
Private Sub Form_Load()
frmColfax.Top = (Screen.Height - frmColfax.Height) / 2
frmColfax.Left = (Screen.Width - frmColfax.Width) / 2
dgdPaie.Caption = dgdPaie.Caption & " - " & Date
End Sub
29
7. (Découverte) lb7Fait
lb7Fait.frm
Option Explicit
Private Sub dgdCom_OnAddNew()
Dim objChamps As Object
Set objChamps = adoCom.Recordset.Fields
objChamps("fldDate") = Date
End Sub
Private Sub Form_Load()
frmCom.Left = (Screen.Width - frmCom.Width) / 2
frmCom.Top = (Screen.Height - frmCom.Height) / 2
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmCom
End Sub
30
8. (Découverte) lb8Fait
lb8Fait.frm
Option Explicit
Private Sub dgdPrix_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Recordset
Set objChamps = adoPrix.Recordset
objChamps("fldNouvPrix") = Round(objChamps.Fields("fldPrixAct") * _
(1 + objChamps.Fields("fldPourcent")), 2)
End Sub
Private Sub Form_Load()
frmPrix.Left = (Screen.Width - frmPrix.Width) / 2
frmPrix.Top = (Screen.Height - frmPrix.Height) / 2
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim intX As Integer, objChamps As Object
Set objChamps = adoPrix.Recordset.Fields
adoPrix.Recordset.MoveFirst
For intX = 1 To adoPrix.Recordset.RecordCount
objChamps("fldPrixAct") = objChamps("fldNouvPrix")
objChamps("fldNouvPrix") = 0
objChamps("fldPourcent") = 0
adoPrix.Recordset.MoveNext
Next intX
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
31
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmPrix
End Sub
Leçon C
Exercices :
1. lc1Fait
lc1Fait.frm
Option Explicit
Dim sngTable(1 To 3, 1 To 4) As Single
Private Sub dgdPaie_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object
'créer une variable objet
Set objChamps = adoPaie.Recordset.Fields
'affecter la référence d'objet
'calculer la paie brute, l'assurance-emploi et la paie net
If objChamps("fldHeures") <= 40 Then
objChamps("fldBrut") = Round(objChamps("fldHeures") * objChamps("fldTaux"), 2)
Else
objChamps("fldBrut") = Round(40 * objChamps("fldTaux") + (objChamps("fldHeures") 40) * objChamps("fldTaux") * 1.5, 2)
End If
32
objChamps("fldIf") = Round(udfCalcImpot(sngTable(), objChamps), 2)
objChamps("fldAe") = Round(objChamps("fldBrut") * 0.0255, 2)
objChamps("fldNet") = Round(objChamps("fldBrut") - objChamps("fldAe") objChamps("fldIf"), 2)
End Sub
Private Sub Form_Load()
Dim intLigne As Integer, intCol As Integer
frmColfax.Top = (Screen.Height - frmColfax.Height) / 2
frmColfax.Left = (Screen.Width - frmColfax.Width) / 2
Open "A:\Tut10\impôt.txt" For Input As #1 'ouvrir le fichier
For intLigne = 1 To 3
For intCol = 1 To 4
'remplir la table
Input #1, sngTable(intLigne, intCol)
Next intCol
Next intLigne
Close #1
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmColfax
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Function udfCalcImpot(sngTab() As Single, objTemp As Object)
Dim intX As Integer, blnTrouve As Boolean
Dim sngImposable As Single, sngImpot As Single
intX = 1
'numéro de ligne
blnTrouve = False
'indique si la tranche d'imposition est
trouvée
'calculer le montant imposable
sngImposable = objTemp("fldBrut") - 124.15
'déduction de base
If objTemp("fldConjoint") = "O" Then
sngImposable = sngImposable - 103.46
'montant pour conjoint
End If
If sngImposable < 0 Then
'le montant imposable peut être inférieur
à0
sngImposable = 0
'l'impôt ne sera pas négatif
End If
'recherche de la tranche d'imposition
Do While intX <= 3 And blnTrouve = False
If sngTab(intX, 1) >= sngImposable Then
'intervalle trouvé
sngImpot = sngTab(intX, 2) + sngTab(intX, 3) * (sngImposable - sngTab(intX, 4))
blnTrouve = True
Else
intX = intX + 1
'recherche dans la ligne suivante
End If
33
Loop
udfCalcImpot = sngImpot
End Function
2. lc2Fait
lc2Fait.frm
Option Explicit
Dim udtTable(1 To 3) As TableTaxe
Private Sub dgdPaie_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object
'créer une variable objet
Set objChamps = adoPaie.Recordset.Fields
'affecter la référence d'objet
'calculer la paie brute, l'assurance-emploi et la paie net
objChamps("fldBrut") = Round(objChamps("fldHeures") * objChamps("fldTaux"), 2)
objChamps("fldIf") = Round(udfCalcImpot(udtTable(), objChamps), 2)
objChamps("fldAe") = Round(objChamps("fldBrut") * 0.0255, 2)
objChamps("fldNet")
=
Round(objChamps("fldBrut")
objChamps("fldAe")
objChamps("fldIf"), 2)
End Sub
-
Private Sub Form_Load()
Dim intLigne As Integer
frmColfax.Top = (Screen.Height - frmColfax.Height) / 2
34
frmColfax.Left = (Screen.Width - frmColfax.Width) / 2
Open "A:\Tut10\impôt.txt" For Input As #1 'ouvrir le fichier
For intLigne = 1 To 3
'remplir la table
Input #1, udtTable(intLigne).sngMax, udtTable(intLigne).sngBase, _
udtTable(intLigne).sngPourcent, udtTable(intLigne).sngSurplus
Next intLigne
Close #1
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmColfax
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Function udfCalcImpot(udtTab() As TableTaxe, objTemp As Object) As Single
Dim intX As Integer, blnTrouve As Boolean
Dim sngImposable As Single, sngImpot As Single
intX = 1
'numéro de ligne
blnTrouve = False
'indique si la tranche d'imposition est
trouvée
'calculer le montant imposable
sngImposable = objTemp("fldBrut") - 124.15
'déduction de base
If objTemp("fldConjoint") = "O" Then
sngImposable = sngImposable - 103.46
'montant pour conjoint
End If
If sngImposable < 0 Then
'le montant imposable peut être inférieur
à0
sngImposable = 0
'l'impôt ne sera pas négatif
End If
'recherche de la tranche d'imposition
Do While intX <= 3 And blnTrouve = False
If udtTab(intX).sngMax >= sngImposable Then
'intervalle trouvé
sngImpot = udtTab(intX).sngBase + udtTab(intX).sngPourcent _
* (sngImposable - udtTab(intX).sngSurplus)
blnTrouve = True
Else
intX = intX + 1
'recherche dans la ligne suivante
End If
Loop
udfCalcImpot = sngImpot
End Function
35
lc2Fait.bas
Option Explicit
Type TableTaxe
sngMax As Single
sngBase As Single
sngPourcent As Single
sngSurplus As Single
End Type
3. lc3Fait
lc3Fait.frm
Option Explicit
Dim sngTable(1 To 3, 1 To 4) As Single
Private Sub dgdPaie_AfterColUpdate(ByVal ColIndex As Integer)
Dim objChamps As Object
'créer une variable objet
Set objChamps = adoPaie.Recordset.Fields
'affecter la référence d'objet
'calculer la paie brute, l'assurance-emploi et la paie net
objChamps("fldBrut") = Round(objChamps("fldHeures") * objChamps("fldTaux"), 2)
objChamps("fldIf") = Round(udfCalcImpot(sngTable(), objChamps), 2)
objChamps("fldAe") = Round(objChamps("fldBrut") * 0.0255, 2)
objChamps("fldNet") = Round(objChamps("fldBrut") - objChamps("fldAe") objChamps("fldIf"), 2)
End Sub
36
Private Sub Form_Load()
Dim intLigne As Integer, intCol As Integer
frmColfax.Top = (Screen.Height - frmColfax.Height) / 2
frmColfax.Left = (Screen.Width - frmColfax.Width) / 2
Open "A:\Tut10\impôt2.txt" For Input As #1 'ouvrir le fichier
For intLigne = 1 To 3
For intCol = 1 To 4
'remplir la table
Input #1, sngTable(intLigne, intCol)
Next intCol
Next intLigne
Close #1
End Sub
Private Sub mnuFichierQuitter_Click()
Unload frmColfax
End Sub
Private Sub mnuFichierImprimer_Click()
PrintForm
End Sub
Private Function udfCalcImpot(sngTab() As Single, objTemp As Object)
Dim intX As Integer, blnTrouve As Boolean
Dim sngImposable As Single, sngImpot As Single
intX = 1
'numéro de ligne
blnTrouve = False
'indique si la tranche d'imposition est
trouvée
'calculer le montant imposable
sngImposable = objTemp("fldBrut") - 124.15
'déduction de base
If objTemp("fldConjoint") = "O" Then
sngImposable = sngImposable - 103.46
'montant pour conjoint
End If
If sngImposable < 0 Then
'le montant imposable peut être inférieur à 0
sngImposable = 0
'l'impôt ne sera pas négatif
End If
'recherche de la tranche d'imposition
Do While intX <= 3 And blnTrouve = False
If sngTab(intX, 1) >= sngImposable Then
'intervalle trouvé
sngImpot = sngTab(intX, 2) + sngTab(intX, 3) * (sngImposable - sngTab(intX, 4))
blnTrouve = True
Else
intX = intX + 1
'recherche dans la ligne suivante
End If
Loop
udfCalcImpot = sngImpot
End Function
37
Impôt2.txt
569.04,0,.17,0
1138.08,96.73,.26,569.04
99999,244.69,.29,1138.08
Débogage
Exercices
3. deb3Fait.frm
Option Explicit
Private Sub cmdCalc_Click()
Dim intX As Integer, intNombre As Integer
Dim intTotNum As Integer, sngMoyenne As Single
Cls 'efface le texte imprimé avec la méthode Print
For intX = 1 To 2 Step 1
intNombre = Val(InputBox("Nombre:", "Saisie d'un nombre"))
Print intNombre
intTotNum = intTotNum + intNombre
Next intX
sngMoyenne = intTotNum / (intX - 1)
Print "La moyenne est: "; sngMoyenne
End Sub
Private Sub cmdQuitter_Click()
Unload frmDebog3
End Sub
Private Sub Form_Load()
frmDebog3.Left = (Screen.Width - frmDebog3.Width) / 2
frmDebog3.Top = (Screen.Height - frmDebog3.Height) / 2
End Sub
38

Documents pareils