|
FICHIER SÉQUENTIEL
Dans un fichier séquentiel, vous stockez vos données un peu comme dans
une liste ou une table. Il contient des enregistrements avec plusieurs
champs (ou valeurs).
Les enregistrements sont écrits et lus ligne par ligne, ils n'ont pas
forcément la même longueur, ni le même nombre de champ (bien que pour
une meilleure gestion il soit préférable de les construire sur un même
nombre de champ).
Vous ne pouvez pas vous placez sur un enregistrement précis, vous êtes
obligé de lire tous les enregistrements du 1er à celui
recherché.
Pour ouvrir un fichier en lecture ou écriture, vous devez connaître au
préalable le prochain numéro de fichier libre, ce numéro permet ensuite
de différencier les différents fichiers ouverts.
Lors de l'ouverture, si le fichier n'existe pas, il est créé.
Public Sub AjoutDonnee(strContent As String,
strChemFich As String)
Dim F As Integer
F = FreeFile
Open strChemFich For Append As #F
Print #F, strContent
Close #F
End Sub
Public Sub AjoutValeurs(Val1 As String, Val2 As Integer, Val3 As Date,
strChemFich As String)
Dim F As Integer
F = FreeFile
Open strChemFich For Append As #F 'ouvert en
ajout
Write #F, Val1, Val2, Val3
Close #F
End Sub
Sub EcritSequentiel()
AjoutDonnee "Ceci est mon 1er essai
d'écriture", "C:\Documents and Settings\Cathy\Mes
documents\Formation\Internet\Vba\Seq1.txt"
AjoutDonnee "Ceci est ma 2ème ligne",
"C:\Documents and Settings\Cathy\Mes documents\Formation\Internet\Vba\Seq1.txt"
AjoutValeurs "Monier", 1, Date - 2,
"C:\Documents and Settings\Cathy\Mes documents\Formation\Internet\Vba\Seq2.txt"
AjoutValeurs "Cathy", 2, Date - 1,
"C:\Documents and Settings\Cathy\Mes documents\Formation\Internet\Vba\Seq2.txt"
AjoutValeurs "Monier Cathy", 3, Date,
"C:\Documents and Settings\Cathy\Mes documents\Formation\Internet\Vba\Seq2.txt"
End Sub
Public Sub LitDonnee(strChemFich As String)
Dim F As Integer
Dim strLigne As String
F = FreeFile
Open strChemFich For Input As #F
While Not EOF(F)
Line Input #F,
strLigne
MsgBox strLigne
Wend
Close #F
End Sub
Public Sub LitValeurs(strChemFich As String)
Dim F As Integer
Dim Val1 As String, Val2 As Integer, Val3 As Date
F = FreeFile
Open strChemFich For Input As #F
While Not EOF(F)
Input #F, Val1,
Val2, Val3 correspondantes
MsgBox Val1 &
" - " & Val2 & " - " & Val3
Wend
Close #F
End Sub
Sub LitSequentiel()
LitDonnee "C:\Documents and Settings\Cathy\Mes
documents\Formation\Internet\Vba\Seq1.txt"
LitValeurs "C:\Documents and Settings\Cathy\Mes
documents\Formation\Internet\Vba\Seq2.txt"
End Sub
Sub RemplaceEnreg()
Dim Fl As Integer, Fe As Integer
Dim Val1 As String, Val2 As Integer, Val3 As Date
Dim strChemFich As String, strChemFichTmp As
String
strChemFich = "C:\Documents and Settings\Cathy\Mes
documents\Formation\Internet\Vba\Seq2.txt"
strChemFichTmp = "C:\Documents and Settings\Cathy\Mes
documents\Formation\Internet\Vba\Seq2.tmp"
Fl = FreeFile
Open strChemFich For Input As #Fl
While Not EOF(Fl)
Input #Fl, Val1,
Val2, Val3
If Val2 = 2 Then
Val1 = "Catherine"
Val3 = Date + 10
End If
Fe = FreeFile
Open strChemFichTmp
For Append As #Fe
Write #Fe, Val1,
Val2, Val3
Close #Fe
Wend
Close #Fl
Kill strChemFich
Name strChemFichTmp As strChemFich
End Sub
Sub EffaceContenu(strContent As String, Val1 As String, Val2 As Integer,
Val3 As Date, strChemFich1 As String, strChemFich2 As String)
Dim F As Integer
F = FreeFile
Open strChemFich1 For Output As #F
Print #F, strContent
Close #F
F = FreeFile
Open strChemFich2 For Output As #F
Write #F, Val1, Val2, Val3
Close #F
End Sub
Sub EcritSeqRemplacement()
EffaceContenu "Ceci est un
remplacement", _
"TERRIER", 100, Date + 10, _
"C:\Documents and Settings\Cathy\Mes documents\Formation\Internet\Vba\Seq1.txt",
_
"C:\Documents and Settings\Cathy\Mes documents\Formation\Internet\Vba\Seq2.txt"
End Sub
Après l'utilisation de ce dernier code, lancez LitSequentiel pour
constater le changement
|