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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| 'CreeChamp(odb,Table,nv Y/N,Champ,Typ (dbX),lg,Auto,Required Y/N,Vide Y/N,Defaut Str,Valid Str,Descr,Clef/Primaire/Unique,RT,RC)
'RT : table qui contient la clé primaire, RC : nom de la clé primaire dans cette table (identique par défaut)
'9.42 Création de tables, champs, index et ajoute une ligne au .ini - source :
'http://warin.developpez.com/access/dao/?page=partie_4#L4.4.4.1
Public Sub CreeChamp(odb, table As String, nv As Boolean, champ As String, typ As Integer, lg As Integer, auto As Boolean _
, req As Boolean, vide As Boolean, defaut As String, Valid As String, Descr As String, Optional clef As String = "" _
, Optional RT As String = "", Optional rc As String = "")
If Not Mode_debug Then On Error GoTo err
Dim oTbl As DAO.TableDef, oFld As DAO.Field, prp As DAO.Property
Dim FSO As New Scripting.FileSystemObject, FileText As Scripting.TextStream, T As String
102 If nv Then
'Ouvre le .ini et ajoute une ligne - 12.2f nom variable
104 Set FileText = FSO.OpenTextFile(Planet_ini, ForAppending, False)
106 FileText.WriteLine table & Space(15 - Len(table)) & "=*" '15.9 même source que la table précédente par défaut - ald Planet_db
108 FileText.Close: Set FileText = Nothing
109 Set FSO = Nothing
End If
110 If msql Then 'TODO vide et auto ne sont pas pris en charge ici !
120 If nv Then
122 Sr = "CREATE TABLE [dbo].[" & table & "] ([" & champ & "]"
Else
130 Sr = "ALTER TABLE [dbo].[" & table & "] ADD [" & champ & "]"
End If
140 Select Case typ
Case dbInteger: Sr = Sr & " smallint"
Case dbLong: Sr = Sr & " int"
Case dbText: Sr = Sr & " nvarchar(" & lg & ")"
Case dbMemo: Sr = Sr & " nvarchar(max)"
Case dbBoolean: Sr = Sr & " bit"
Case dbSingle: Sr = Sr & " real"
Case dbDouble: Sr = Sr & " float"
Case dbDate: Sr = Sr & " datetime"
Case dbLongBinary: Sr = Sr & " varbinary(max)" '16.8a ald " image"
Case Else: Call message("erreur " & typ & " non prévu dans fonctions.creechamp" & table & "." & champ)
End Select
150 Sr = Sr & IIf(req, " NOT ", "") & " NULL" 'attention si NOT NULL la table doit être vide !
152 If defaut > " " Then
154 If IsNumeric(defaut) Then Sr = Sr & " DEFAULT (" & defaut & ")" Else Sr = Sr & " DEFAULT ('" & defaut & "')"
End If
160 If nv Then 'exemple copié depuis la pk de HG.dbo.stock : déclare la clé primaire
162 Sr = Sr & ", CONSTRAINT [pk_" & table & "] PRIMARY KEY CLUSTERED ([" & champ & "] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]"
End If
' MsgBox Sr
170 cnx.Execute Sr
'description pour info - TODO : validation, contraintes
172 Sr = "sp_addextendedproperty @name = 'Description', @value = '" & Apo2(Descr) & "', @level0type = 'Schema', @level0name = 'dbo'" _
& ", @level1type = 'Table', @level1name = '" & table & "', @level2type = 'Column', @level2name = '" & champ & "';"
174 cnx.Execute Sr
Else
212 If nv Then Set oTbl = odb.CreateTableDef(table) Else Set oTbl = odb.TableDefs(table)
214 If typ = dbText Then Set oFld = oTbl.CreateField(champ, typ, lg) Else Set oFld = oTbl.CreateField(champ, typ)
216 If auto Then oFld.Attributes = dbAutoIncrField 'Définit le champ en numero_auto
218 oFld.Required = req 'Null interdit ?
220 If typ = dbText Then oFld.AllowZeroLength = vide 'chaine vide autorisée ?
222 If Len(defaut) > 0 Then oFld.DefaultValue = defaut
224 If Len(Valid) > 0 Then oFld.ValidationRule = Valid
226 oTbl.Fields.Append oFld 'Ajoute le champ à la table
228 If nv Then odb.TableDefs.Append oTbl 'Ajoute la table à la base de données
230 If Nz(Descr) > " " Then
232 Set prp = oFld.CreateProperty("Description", dbText, Descr)
234 oFld.Properties.Append prp
End If
236 If typ = dbBoolean Then
238 Set prp = oFld.CreateProperty("Format", dbText, "Yes/No")
240 oFld.Properties.Append prp 'Format oui/non
242 Set prp = oFld.CreateProperty("DisplayControl", dbInteger, 106)
244 oFld.Properties.Append prp 'case à cocher
End If
246 oTbl.Fields.refresh 'Rafraichit la collection
248 odb.TableDefs.refresh
254 Set prp = Nothing
256 Set oFld = Nothing
258 Set oTbl = Nothing
300 If Len(RT) > 0 Then Call CreeRelation(odb, table, champ, RT, rc) 'TODO MSQL
End If
302 If Len(clef) = 1 Then Call CreeIndex(odb, table, champ, clef)
Exit Sub
err: Call message("Erreur " & err.Number & "/" & Erl & " dans fonctions.CreeChamp " & table & "." & champ & " : " & err.description)
End Sub
'10.1e ajout d'un index sur un champ
Public Sub CreeIndex(odb, table As String, champ As String, clef As String)
If Not Mode_debug Then On Error GoTo err
Dim oTbl As DAO.TableDef, oFld As DAO.Field, oind As DAO.Index
100 If msql Then
102 If clef = "C" Or clef = "U" Then 'index : le 1er champ est d'office la PK
104 Sr = "CREATE" & IIf(clef = "U", "UNIQUE", "") & " NONCLUSTERED INDEX [" & table & "_" & champ & "] ON [dbo].[" & table & "] ([" & champ _
& "] ASC) WITH (PAD_INDEX=OFF, STATISTICS_NORECOMPUTE=OFF, SORT_IN_TEMPDB=OFF, DROP_EXISTING=OFF, ONLINE=OFF, ALLOW_ROW_LOCKS=ON, ALLOW_PAGE_LOCKS=ON) ON [PRIMARY]"
' MsgBox Sr
106 cnx.Execute Sr
End If
Else
112 Set oTbl = odb.TableDefs(table)
114 Set oind = oTbl.CreateIndex(champ) 'Crée l'index du même nom
116 Set oFld = oind.CreateField(champ)
118 oind.Fields.Append oFld 'Ajoute le champ à la collection Fields
120 Select Case clef
Case "C": oind.Unique = False 'standard
Case "P": oind.Primary = True 'primaire
Case "U": oind.Unique = True 'unique
Case Else: Call message("cas non prévu en création de la clé " & champ & " sur " & table)
End Select
122 oTbl.Indexes.Append oind 'Ajoute l'index à la table
124 oTbl.Indexes.refresh 'Rafraichit la collection
126 Set oFld = Nothing
128 Set oind = Nothing
130 Set oTbl = Nothing
End If
Exit Sub
err: Call message("Erreur " & err.Number & "/" & Erl & " dans fonctions.CreeIndex sur " & table & "." & champ & " : " & err.description)
End Sub
'10.1f ajout d'une relation entre 2 champs RT = table maitresse, RC = champ maitre si nom différent
Public Sub CreeRelation(odb, table As String, champ As String, RT As String, Optional rc As String = "")
If Not Mode_debug Then On Error GoTo err
Dim oFld As DAO.Field, oRlt As DAO.Relation, c As String
100 c = IIf(Len(rc) > 0, rc, champ)
102 Set oRlt = odb.CreateRelation(RT & "_" & table & "_" & champ, RT, table, dbRelationUpdateCascade) 'Crée la relation
104 Set oFld = oRlt.CreateField(c)
106 oFld.ForeignName = champ 'Définit la clé externe
108 oRlt.Fields.Append oFld 'Ajoute le champ
110 odb.Relations.refresh 'Rafraîchit la collection Relations
112 odb.Relations.Append oRlt 'Ajoute la relation
114 odb.Relations.refresh 'Rafraîchit la collection Relations
116 Set oRlt = Nothing
118 Set oFld = Nothing
Exit Sub
err: Call message("Erreur " & err.Number & "/" & Erl & " dans fonctions.CreeRelation sur " & table & "." & champ & " : " & err.description)
End Sub |
Partager