Scinder un champ
Scinder un champ
Dans une table mal réalisée, on peut trouver des champs non atomique. On cherchera donc à scinder ce champ en ajoutant deux nouveaux champs à la table.
Un champ non atomique est par exemple un champ [Nom et prénom] lorsque ce champ doit permettre la sélection de tous les "Dupont".
L'utilisation de la fonction fnFieldSplit() permettra de simplifier le scindage du champ à problème.
Syntaxe :
fnFieldSplit "LaTable", "AncienChamp", "Champ1", "Champ2", ";"
ou si l'on souhaite scinder sur une nombre de caractères :
fnFieldSplit "LaTable", "AncienChamp", "Champ1", "Champ2", ,3
Function fnFieldSplit(sTable As String, _
sFieldToSplit As String, _
sNewField1 As String, _
sNewField2 As String, _
Optional sSeparator As String, _
Optional iChar As Integer)
Dim DB As DAO.Database
Dim RS As DAO.Recordset
Dim sC1 As String, sC2 As String
Dim Pos As Long
Dim sFd As String
Set DB = CurrentDb
If sSeparator = "" Then sSeparator = " "
'ajout des deux champs
CurrentDb.Execute "ALTER TABLE [" & sTable & "] " _
& "ADD COLUMN [" & sNewField1 & "] TEXT;"
CurrentDb.Execute "ALTER TABLE [" & sTable & "] " _
& "ADD COLUMN [" & sNewField2 & "] TEXT;"
Set RS = DB.OpenRecordset(sTable)
'boucle sur les enregistrements
Do Until RS.EOF
sFd = Nz(RS(sFieldToSplit), "")
If iChar > 0 Then
sC1 = Trim(Left(sFd, iChar))
sC2 = Trim(Mid(sFd, iChar + 1))
Else
Pos = Nz(InStr(sFd, sSeparator), 0)
If Pos > 0 Then
sC1 = Trim(Left(sFd, Pos - 1))
sC2 = Trim(Mid(sFd, Pos + 1))
Else
sC1 = Trim(sFd)
sC2 = ""
End If
End If
RS.Edit
RS(sNewField1) = sC1
RS(sNewField2) = sC2
RS.Update
RS.MoveNext
Loop
'libérer
RS.Close
DB.Close
Set RS = Nothing
Set DB = Nothing
Exit Function
End Function
Dernière modification : 11/01/2013 00:18
Catégorie : - Tables
Page lue 6859 fois