Accueil > > > EXTRACTION DEPUIS UN FICHIER TEXTE ET REMPLISSAGE D'UN BASE DE DONNEES ACCESS EXISTANCES
EXTRACTION DEPUIS UN FICHIER TEXTE ET REMPLISSAGE D'UN BASE DE DONNEES ACCESS EXISTANCES
Information sur la source
Description
Ce code est là à titre d'exemple pour ceux qui se posent deux questions : 1) Comment j'extrait une chaîne de taille variable ? (Rq : la mettre en début de ligne) 2) C'est comment qu'on fait pour remplir une base de données ACCESS existante ? Special thanks to Processus qui m'a permis d'éviter de faire gratter mon disque dur pour faire l'extraction. PS: ce code fonctionne avec la bibliothèque DAO 3.51 de Microsoft l'exemple est tiré d'une appli pour mon boulot, alors pas de question du genre (qu'est ce qui veux avec ses palettes?) une dernière chose, la base doit être vide pour qu'il fonctionne sinon vous changer les données du fichier texte.
Source
- Dim fs As New FileSystemObject
- Dim db As DAO.Database
- Dim rc As DAO.Recordset
- Dim fil As String, rep As String
- Dim str As String
- Dim i As Integer
- Dim strtable(1 To 13) As String
- Dim start, pause
-
-
-
- Private Sub transfert_Click()
-
- Dim strtbl
-
- pause = 2
- Form2.Show
- ' parametre du chemin et du fichier source
- rep = slash(Dir.Path)
- fil = rep & File
- 'ouverture de la dase de donnees
- Set db = OpenDatabase(rep & "palette.mdb")
-
- Form2.work.Caption = "Extraction des données..."
- Form2.Refresh
-
- 'On extrait les donnees
- Open fil For Input As #1
- i = 1
- Do While Not EOF(1)
- Line Input #1, str
- strtbl = Split(str)
- str = Trim(strtbl(0))
- Debug.Print i & " " & str
- stock str, i 'pour ranger les donnees extraites
- str = ""
- i = i + 1
- Loop
- Close #1
-
- 'une pause pour faire beau
- start = Timer
- Do While Timer < start + pause: Loop
- Form2.work.Caption = "Transfert des données..."
- Form2.Refresh
-
- 'on remplit la première table de la base
- Set rc = db.OpenRecordset("palette", dbOpenTable)
- rc.AddNew
- For i = 1 To 7
- rc.Fields(i - 1).Value = strtable(i)
- Next i
- rc.Update
- rc.Close
-
- Set rc = Nothing
-
- 'on remplit la 2eme table de la base
- Set rc = db.OpenRecordset("caisses")
- For i = 0 To 4 Step 2
- rc.AddNew
- rc.Fields(0).Value = strtable(1)
- rc.Fields(2).Value = strtable(i + 8)
- rc.Fields(1).Value = strtable(i + 9)
- rc.Update
- Next i
-
- rc.Close
-
- db.Close
- Set rc = Nothing
- Set db = Nothing
-
- ' du remplissage pour le fun
- start = Timer
- Do While Timer < start + pause: Loop
- Form2.work.Caption = "Fin du travail"
- Form2.Refresh
- start = Timer
- Do While Timer < start + pause: Loop
- Unload Form2
-
- End Sub
-
- Private Sub Dir_Change()
- File.Path = Dir.Path
- File.Refresh
- End Sub
-
- Private Sub Drive_Change()
- Dir.Path = Drive
- Dir.Refresh
- End Sub
-
- ' un test sur "\" dans le chemin de fichier
- Function slash(pathname As String) As String
- If Right(Path, 1) <> "\" Then
- slash = pathname + "\"
- Else
- slash = pathname
- End If
- End Function
-
- 'Faut bien mettre les donnees quelque part
- 'Un tableau est une idée
- Sub stock(str As String, i As Integer)
- If i > 7 Then GoTo Choix Else GoTo Simple
-
- Choix: Select Case i
- Case 8: strtable(8) = Left(str, 2)
- strtable(9) = Right(str, 2)
- Case 9: strtable(10) = Left(str, 2)
- strtable(11) = Right(str, 2)
- Case 10: strtable(12) = Left(str, 2)
- strtable(13) = Right(str, 2)
- End Select
- Exit Sub
-
- Simple: strtable(i) = str
-
- End Sub
-
-
Dim fs As New FileSystemObject
Dim db As DAO.Database
Dim rc As DAO.Recordset
Dim fil As String, rep As String
Dim str As String
Dim i As Integer
Dim strtable(1 To 13) As String
Dim start, pause
Private Sub transfert_Click()
Dim strtbl
pause = 2
Form2.Show
' parametre du chemin et du fichier source
rep = slash(Dir.Path)
fil = rep & File
'ouverture de la dase de donnees
Set db = OpenDatabase(rep & "palette.mdb")
Form2.work.Caption = "Extraction des données..."
Form2.Refresh
'On extrait les donnees
Open fil For Input As #1
i = 1
Do While Not EOF(1)
Line Input #1, str
strtbl = Split(str)
str = Trim(strtbl(0))
Debug.Print i & " " & str
stock str, i 'pour ranger les donnees extraites
str = ""
i = i + 1
Loop
Close #1
'une pause pour faire beau
start = Timer
Do While Timer < start + pause: Loop
Form2.work.Caption = "Transfert des données..."
Form2.Refresh
'on remplit la première table de la base
Set rc = db.OpenRecordset("palette", dbOpenTable)
rc.AddNew
For i = 1 To 7
rc.Fields(i - 1).Value = strtable(i)
Next i
rc.Update
rc.Close
Set rc = Nothing
'on remplit la 2eme table de la base
Set rc = db.OpenRecordset("caisses")
For i = 0 To 4 Step 2
rc.AddNew
rc.Fields(0).Value = strtable(1)
rc.Fields(2).Value = strtable(i + 8)
rc.Fields(1).Value = strtable(i + 9)
rc.Update
Next i
rc.Close
db.Close
Set rc = Nothing
Set db = Nothing
' du remplissage pour le fun
start = Timer
Do While Timer < start + pause: Loop
Form2.work.Caption = "Fin du travail"
Form2.Refresh
start = Timer
Do While Timer < start + pause: Loop
Unload Form2
End Sub
Private Sub Dir_Change()
File.Path = Dir.Path
File.Refresh
End Sub
Private Sub Drive_Change()
Dir.Path = Drive
Dir.Refresh
End Sub
' un test sur "\" dans le chemin de fichier
Function slash(pathname As String) As String
If Right(Path, 1) <> "\" Then
slash = pathname + "\"
Else
slash = pathname
End If
End Function
'Faut bien mettre les donnees quelque part
'Un tableau est une idée
Sub stock(str As String, i As Integer)
If i > 7 Then GoTo Choix Else GoTo Simple
Choix: Select Case i
Case 8: strtable(8) = Left(str, 2)
strtable(9) = Right(str, 2)
Case 9: strtable(10) = Left(str, 2)
strtable(11) = Right(str, 2)
Case 10: strtable(12) = Left(str, 2)
strtable(13) = Right(str, 2)
End Select
Exit Sub
Simple: strtable(i) = str
End Sub
Conclusion
Ne vous géner pas et donner votre avis, ca peut motiver à mettre d'autres sources, bien meilleures je l'espère.
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|