cs_iubito
Messages postés629Date d'inscriptionmercredi 3 juillet 2002StatutMembreDernière intervention 9 octobre 2006
-
22 août 2002 à 16:00
MysTiK74
Messages postés32Date d'inscriptionvendredi 23 août 2002StatutMembreDernière intervention 7 mars 2003
-
24 août 2002 à 16:36
Bonjour @ tous !
Voilà g 1 truc à faire que je sais bien faire en Access (base de données+formulaire+VBA) mais pas en VB pur.
J'ai dans un fichier .txt la structure suivante :
nom:machine
(nom d'une personne et nom de son ordi sur le réseau).
1. Je veux lire ce fichier et le balancer dans 2 tableaux "nom" et "machine". Comment faire :question)
2. Ensuite je veux faire apparaître les noms dans une liste déroulante (j'ai posé une combobox sur ma form). Comment faire pour mettre les éléments de la liste nom(1), nom(2).... :question)
3. Enfin, je veux récupérer le rang sélectionné. Si rang=1 alors la machine de la personne sélectionnée est machine(1). J'espère que ch'uis clair :). Comment faire :question)
cs_thitoune
Messages postés124Date d'inscriptionmardi 19 février 2002StatutMembreDernière intervention 7 mars 20042 22 août 2002 à 18:07
Je t'ai développé ce qui suit, ca te permet de faire ce que tu veux :
Dim tabNom() As String, tabMachine() As String
Dim nbr As Integer
Private Sub Combo1_Change()
i = ComboBox1.ListIndex
MsgBox (tabNom(i))
End Sub
Private Sub Form_Load()
Call insertionTableau
For i = 0 To nbr - 1
Combo1.AddItem tabNom(i)
Combo2.AddItem tabMachine(i)
Next i
End Sub
Private Sub UserForm_Click()
End Sub
Sub insertionTableau()
Dim TextLine As String
nbr = 0
Open "c:\stage\prout.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, TextLine
tt1 = spliter(TextLine, 0)
tt2 = spliter(TextLine, 1)
ReDim Preserve tabNom(nbr)
ReDim Preserve tabMachine(nbr)
tabNom(nbr) = tt1
tabMachine(nbr) = tt2
nbr = nbr + 1
Loop
Close #1
End Sub
'permet de récupérer l'élément 0 ou 1 dans la chaine délimitée par ':'
Function spliter(s As String, elem As Integer) As String
Dim count As Integer
Dim idx As Integer
Dim deb As Integer, fin As Integer
count = 0
idx = 0
deb = -1
fin = -1
If s = "" Then
spliter = ""
Exit Function
End IfIf elem 0 Then deb 0
Do
If Mid$(s, idx + 1, 1) = ":" Then
count = count + 1
If deb = -1 Then
deb = idx + 1
Else
fin = idx
End If
End If
idx = idx + 1
Loop While fin = -1 And idx < Len(s)
If fin -1 Then fin idx
spliter = Mid$(s, deb + 1, fin - deb)
End Function
Dim fso As New FileSystemObject
Dim intPosition As Integer
Dim intResultat1 As String
Dim intResultat2 As String
Dim cpt_1 As Integer
Dim cpt_2 As Integer
Dim tab_1() As String
Dim tab_2() As String
Private Sub Combo1_Click()
If Combo1.ListIndex <> -1 Then
Combo2.ListIndex = Combo1.ListIndex
Text1.Text = Combo1.List(Combo1.ListIndex)
Text2.Text = Combo2.List(Combo1.ListIndex)
End If
End Sub
Private Sub Combo2_Click()
If Combo2.ListIndex <> -1 Then
Combo1.ListIndex = Combo2.ListIndex
Text1.Text = Combo1.List(Combo2.ListIndex)
Text2.Text = Combo2.List(Combo2.ListIndex)
End If
End Sub
Private Sub Form_Load()
On Error GoTo traite_erreur
'Lecture fichier ini
If fso.FileExists("C:\test.txt") = True Then
Call LectureFicIni
Call MettreDsCombo
End If
Exit Sub
traite_erreur:
'MsgBox Err.Number
If Err.Number <> 0 Then
MsgBox Err.Number & " : " & Err.Description, vbOKOnly, "Erreur"
Exit Sub
End If
End Sub
Private Sub LectureFicIni()
Dim ficini As TextStream
Dim strTrame As String
Set ficini = fso.OpenTextFile("C:\test.txt", ForReading)
Do Until ficini.AtEndOfStream
strTrame = ficini.ReadLine
intPosition = InStr(strTrame, ":")
If intPosition <> 0 Then
intResultat1 = Left(strTrame, intPosition - 1)
intResultat2 = Right(strTrame, (Len(strTrame) - intPosition))
DeAtHCrAsH
Messages postés2670Date d'inscriptionvendredi 25 janvier 2002StatutMembreDernière intervention 6 février 2013 23 août 2002 à 09:28
Salut,
Si tu veux envoie moi ton fichier txt comportant ta structure a candynman@aol.com, et je te ferais un ptit bout de code qui je pense sera beaucoup plus simple et beaucoup plus court que ceux proposé par NuNu et thitoune...
DeAtHCrAsH
Messages postés2670Date d'inscriptionvendredi 25 janvier 2002StatutMembreDernière intervention 6 février 2013 23 août 2002 à 09:28
Salut,
Si tu veux envoie moi ton fichier txt comportant ta structure a candynman@aol.com, et je te ferais un ptit bout de code qui je pense sera beaucoup plus simple et beaucoup plus court que ceux proposé par NuNu et thitoune...
MysTiK74
Messages postés32Date d'inscriptionvendredi 23 août 2002StatutMembreDernière intervention 7 mars 2003 23 août 2002 à 22:50
Adieu p'tit
Y plus sur mais c'est moins long
utilise les fonctions d'acces aux fichiers
Open "nomdufichier.txt" For Binary Access Read As #1
l = LOF(1) ' taille du fichier
For c = 1 To l
ca = Input(1, #1) ' recuperation 1 caractere
st = st & ca
Next
Close 1
tu peux utiliser des options d'acces differente et recuperer une ligne a la fois
ensuite les fonctions mid et compagnie te permettent de formater ton texte (cool si t'as un séparateur de champ dans ton fichier texte)
ensuite utilise
combobox.additem "chaine de caracteres"
pour ajouter tes champs dans ton combo
cs_iubito
Messages postés629Date d'inscriptionmercredi 3 juillet 2002StatutMembreDernière intervention 9 octobre 2006 24 août 2002 à 15:03
Bein là kom c pas un fichier binaire j'préfère utilise un accès séquentiel car c + facile pour avoir une ligne mais qd c du binaire ouais ta fonction est bien :)
Sinon pour tout le monde : j'vais déposer une source avec ce script là :) merci !