Bonjour,
j'essai de mettre en place une méthode de création/lecture/écriture sur fichier .csv. Je me suis inspiré sur un tuto disponible sur développez.com en utilisant la méthode par "accès direct".
L'erreur affichée est que le "fichier est déjà ouvert" alors que je close le fichier à la fin.
Voici mon code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 Private Sub Lecture(recu() As tRecu) Dim i As Integer Dim szTmp As String Dim nTmp As Integer Dim intFic As Integer If frmMain.lstPeriodes.ListIndex = -1 Then 'Si aucune période n'est selectionnée nTmp = 0 Else nTmp = Val(Trim$(frmMain.lstPeriodes.ListIndex)) End If szTmp = "D:\Documents\s611516\Mes Documents\Level1\" & Trim$(frmMain.szContrat) & "_" & Trim$(Replace(Replace(frmMain.lstPeriodes.List(nTmp), " ", "-"), "/", "")) & "_" & Trim$(nomVariable) & ".csv" If Dir(szTmp) <> "" Then intFic = FreeFile Open szTmp For Output As #intFic 'For Random As intFic Len = Len(recu) For i = 0 To 4 Get #intFic, (i + 1), recu(i) Next i Else Open szTmp For Output As FreeFile For i = 0 To 4 recu(i).Montant = "" recu(i).Date = "" recu(i).Remarque = "" Next i End If Close #intFic End SubJ'ai fias un débeugage et ça s'arrête à la ligne 19 de la méthode Lecture en m'écrivant m'éthode d'accés au fichier incorrect.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 Private Sub Ecriture(numLigne As Integer, recu As tRecu) Dim szTmp As String Dim nTmp As Integer Dim intFic As Integer Dim intNum As Integer If frmMain.lstPeriodes.ListIndex = -1 Then 'Si aucune période n'est selectionnée nTmp = 0 Else nTmp = Val(Trim$(frmMain.lstPeriodes.ListIndex)) End If szTmp = "D:\Documents\s611516\Mes Documents\Level1\" & Trim$(frmMain.szContrat) & "_" & Trim$(Replace(Replace(frmMain.lstPeriodes.List(nTmp), " ", "-"), "/", "")) & "_" & Trim$(nomVariable) & ".csv" If Dir(szTmp) <> "" Then Open szTmp For Random As intFic Len = Len(recu) intNum = LOF(intFic) / Len(recu) 'Compte le nombre d'enregistrements Put intFic, intNum + 1, recu 'Ajoute la donnée Else Open szTmp For Output As FreeFile End If Close intFic End Sub
On ne peut pas utiliser la méthode d'accés direct avec les fichiers .csv contrairement aux fichiers .txt?
Partager