1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| Sub CreerChampSiInexistant(strNomTable As String, strNomChamp As String)
Dim otbl As DAO.TableDef
Dim odb As DAO.Database
Dim strTemp As String
'Accède à la table dans la base de données courante
Set odb = CurrentDb
Set otbl = odb.TableDefs(strNomTable)
'Vérifie si la table est une table attachée
If (otbl.Attributes And dbAttachedTable) Then
'Récupère le nom de la table dans la base distante
strTemp = otbl.SourceTableName
'Ouvre la base distante et accède à la table
Set odb = DBEngine.OpenDatabase(CheminConnect(otbl.Connect), False, False)
Set otbl = odb.TableDefs(strTemp)
End If
'Crée le champ
CreationChamp otbl, strNomChamp
End Sub
Sub CreationChamp(otbl As DAO.TableDef, strNomChamp As String)
On Error GoTo err
With otbl
.Fields.Append .CreateField(strNomChamp, dbText, 50)
End With
fin:
Exit Sub
err:
'Le champ existe déjà, on ne lève pas l'erreur
'Par contre on lève toutes les autres
With err
If .Number <> 3191 Then _
.Raise .Number, .Source, .Description
End With
Resume fin
End Sub
Function CheminConnect(strChemin As String) As String
Dim i As Integer
Dim temp() As String
temp = Split(strChemin, ";")
For i = 0 To UBound(temp)
If temp(i) Like "DATABASE=*" Then
CheminConnect = Trim(Mid(temp(i), 10))
Exit For
End If
Next i
End Function |
Partager