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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
Sub importMSA2()
' déclaration des variables
Dim rstEmploye As ADODB.Recordset
Dim rstSalaire As ADODB.Recordset
Dim rstCotisation As ADODB.Recordset
Dim CType As ADODB.CursorTypeEnum
Dim LType As ADODB.LockTypeEnum
Dim CLocation As ADODB.CursorLocationEnum
Dim Cnct As ADODB.Connection
Dim brutMsa as String
Dim IDEmploye As Integer
Dim IDSalaire As Integer
Dim nomFichierMSA As String
Dim numFichier As Integer
' ouverture du fichier en entrée
nomFichierMSA = tbMSA.Text
numFichier = FreeFile 'Génère un descripteur de fichier libre, Définit un numéro de fichier libre
Open nomFichierMSA For Input As numFichier 'Ouvre le fichier (en mode lecture)
CType = adOpenStatic
LType = adLockOptimistic
CLocation = adUseServer
If Not EOF(numFichier) Then
' instanciation et ouverture de la connexion
Set Cnct = New ADODB.Connection
Cnct.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tbBDD.Text & ";"
Cnct.Open
Line Input #numFichier, vLigne
Set rstEmploye = New ADODB.Recordset
With rstEmploye
.CursorLocation = CLocation
.CursorType = CType
.LockType = LType
Set .ActiveConnection = Cnct 'instance de ADODB.Connection
.Open "SELECT * FROM Employes"
End With
Set rstSalaire = New ADODB.Recordset
With rstSalaire
.CursorLocation = CLocation
.CursorType = CType
.LockType = LType
Set .ActiveConnection = Cnct ' instance de ADODB.Connection
.Open "SELECT * FROM Salaires"
End With
Set rstCotisation = New ADODB.Recordset
With rstCotisation
.CursorLocation = CLocation
.CursorType = CType
.LockType = LType
Set .ActiveConnection = Cnct
.Open "SELECT * FROM Cotisations"
End With
End If
While Not EOF(numFichier)
Line Input #numFichier, vLigne 'Lit une ligne
If Mid(vLigne, 60, 2) = "10" Then
With rstEmploye
.AddNew
.Fields("numSS").Value = Mid(vLigne, 39, 13)
.Fields("nom").Value = Mid(vLigne, 62, 25)
.Update
IDEmploye = .Fields("idEmploye").Value
End With
End If
If Mid(vLigne, 60, 2) = "20" Then
If Mid(vLigne, 39, 13) = "1680544109053" Then
brutMsa = Mid(vLigne, 89, 10)
brutMsa = (Val(brutMsa)) + (Val(Mid(vLigne, 89, 10)))
&&R = (Val(brutMsa)) + (Val(Mid(vLigne, 89, 10)))
MsgBox brutMsa
With rstEmploye
.Fields("brutDernierTrimestre").Value = &&R
.Update
End With
End If
vBrutMSA = CDbl(Mid(vLigne, 88, 11))
With rstSalaire
.AddNew
.Fields("idEmploye").Value = IDEmploye
.Fields("dateSalaire").Value = Mid(vLigne, 52, 8)
.Fields("brutMSA").Value = vBrutMSA
.Update
IDSalaire = .Fields("idSalaire").Value
End With
End If
If Mid(vLigne, 60, 2) = "30" Then
vMontant = CDbl(Mid(vLigne, 87, 12))
With rstCotisation
.AddNew
.Fields("idSalaire").Value = IDSalaire
.Fields("typeCotisation").Value = Mid(vLigne, 62, 5)
.Fields("montant").Value = vMontant
.Update
End With
End If
ProgressBar1.Min = 0
ProgressBar1.Max = rstEmploye.RecordCount
ProgressBar1.Value = 0
ProgressBar1.Value = ProgressBar1.Value + 1
lblPourcentage.Caption = Int((ProgressBar1.Max / ProgressBar1.Value * 100) / 100) & " %"
lblPourcentage.Refresh
Wend
If Not (Cnct Is Nothing) Then
rstEmploye.Close
rstSalaire.Close
rstCotisation.Close
Cnct.Close
Set rstEmploye = Nothing
Set rstSalaire = Nothing
Set rstCotisation = Nothing
Set Cnct = Nothing 'Pour être sur que l'instance de la connexion est bien "libérée" (ce qui est différent de "fermée", la fermeture se faisant via le close)
End If
MsgBox " L'importation MSA est términée"
End Sub |