OVH Cloud OVH Cloud

remplir un champ dont on ne connais pas les nom

3 réponses
Avatar
oualaléreur
Bonjour,

J'importe des donn=E9es d'un fichier texte dont la premi=E8re ligne
contient le nom des champs.
Je souhaite faire cela sans contrainte au niveau de ces noms.Je cr=E9e
la table et les champs, qui prennent bien les bons noms, mais lorsque
je veux remplir cette table avec les donn=E9es contenues dans le reste
du fichier texte, je me heurte =E0 un probl=E8me : je ne connais pas les
noms des diff=E9rents champs.

Comment fait-on pour d=E9signer un champs (ou pour le remplir, si jamais
ma m=E9thode n'=E9tait pas bonne) dont on ne connait pas le nom ?

voila le code correspondant :



F =3D FreeFile
Open sfile For Input As #F

Line Input #F, txtLine ' Make the first line the
current line
vTitle =3D Split(txtLine, Chr(9), , 1) ' Separate the titles of the
different columns and edit vTitle
iColumns =3D UBound(vTitle)
i =3D 0


Do While (i <=3D iColumns) ' go through vTitle to identify
the columns
Set Fld =3D Tbl.CreateField(vTitle(i), dbText, 40)
Tbl.Fields.Append Fld
i =3D i + 1
Loop

dbs.TableDefs.Append Tbl
dbs.TableDefs.Refresh

Line Input #F, txtLine ' Make the next ligne the
current line
Set rst =3D dbs.OpenRecordset("Temp_Tbl_ADHock", dbOpenDynaset)
i =3D 0

Do While Not EOF(F) ' Go through the file to fill
the fields of "Tbl_AdHock"


vLine =3D Split(txtLine, Chr(9), , 1) ' Separate the different
records of the current line and edit vLine

rst.AddNew ' Add a new record in "Temp_Tbl_AdHock" to fill
it with the records identified in the document


Do While (i <=3D iColumns)

Tbl![ben_l=E0_je_sais_pas_quoi_mettre] =3D vLine(i)
<<<<<<<<<<<<<<<<<<<<<<<<<<
i =3D i + 1
Loop

rst.Update
Line Input #F, txtLine ' Make the next ligne the
current line

Loop


merci, et bonne journ=E9e

Benoit

3 réponses

Avatar
oualaléreur
Aprés mûre réflexion, j'ai l'impression qu'il suffirait d'enlever
les guillemets de part et d'autre du nom du champ contenu dans
vTitle(i).
Quelqu'un sait-il comment on fait cela ?

Merci,

Benoit
Avatar
Guy
Bonjour,

J'importe des données d'un fichier texte dont la première ligne
contient le nom des champs.
Je souhaite faire cela sans contrainte au niveau de ces noms.Je crée
la table et les champs, qui prennent bien les bons noms, mais lorsque
je veux remplir cette table avec les données contenues dans le reste
du fichier texte, je me heurte à un problème : je ne connais pas les
noms des différents champs.

Comment fait-on pour désigner un champs (ou pour le remplir, si jamais
ma méthode n'était pas bonne) dont on ne connait pas le nom ?


Bonjour,

en VB les champs sont des objets; ils font partie d'une collection;

Set Tbl = Db.TableDefs(wk$)


Nbcol = Tbl.Fields.Count - 1
For ijk = 0 To Nbcol
/* recup nom des champs */
TbEntete$(ijk) = Tbl.Fields(ijk).Name
/* maj champ sur un nouvel enregistreemnt*/
Tbl.Fields(ijk)='truc'
Next ijk

Bonne chance
GR



voila le code correspondant :



F = FreeFile
Open sfile For Input As #F

Line Input #F, txtLine ' Make the first line the
current line
vTitle = Split(txtLine, Chr(9), , 1) ' Separate the titles of the
different columns and edit vTitle
iColumns = UBound(vTitle)
i = 0


Do While (i <= iColumns) ' go through vTitle to identify
the columns
Set Fld = Tbl.CreateField(vTitle(i), dbText, 40)
Tbl.Fields.Append Fld
i = i + 1
Loop

dbs.TableDefs.Append Tbl
dbs.TableDefs.Refresh

Line Input #F, txtLine ' Make the next ligne the
current line
Set rst = dbs.OpenRecordset("Temp_Tbl_ADHock", dbOpenDynaset)
i = 0

Do While Not EOF(F) ' Go through the file to fill
the fields of "Tbl_AdHock"


vLine = Split(txtLine, Chr(9), , 1) ' Separate the different
records of the current line and edit vLine

rst.AddNew ' Add a new record in "Temp_Tbl_AdHock" to fill
it with the records identified in the document


Do While (i <= iColumns)

Tbl![ben_là_je_sais_pas_quoi_mettre] = vLine(i)
<<<<<<<<<<<<<<<<<<<<<<<<<<
i = i + 1
Loop

rst.Update
Line Input #F, txtLine ' Make the next ligne the
current line

Loop


merci, et bonne journée

Benoit



Avatar
oualaléreur
Merci bien Guy,

c'est plus élégant peut-être, mais entre temps j'avais opté pour
ceci :


Do Until EOF(F) ' Go through the file to fill the
fields of "Temp_Tbl_AdHock"

Line Input #F, txtLine ' Make the next ligne the
current line
vLine = Split(txtLine, Chr(9), , 1) ' Separate the different
records of the current line and edit vLine

rst.AddNew ' Add a new record in
"Temp_Tbl_AdHock"
i = 0


For Each Fld In rst.Fields ' For each field..

If (vLine(i) <> "") Then ' .. If the record exists..
Fld.Value = vLine(i) ' .. Its value is redorded
in the table
End If

If (i < UBound(vLine)) Then ' Case where we are not at the
end of the line
i = i + 1
Else
GoTo nextline
End If

Next Fld

nextline:
rst.Update

Loop


et puis ca marche, alors..

à +

Benoit