PDA

Vollständige Version anzeigen : Felder anhängen an bestehende Tabelle


Claus Rauchschindel
02.01.2002, 09:18
Ein frohes Neues Jahr wünsch ich.

Wie kann ich Felder an eine bestehende Tabelle anhängen.

So gehts irgendwie nicht!

Set tbPinas = db.TableDefs(vFilenameVorne)
With tbPinas
Set fld1 = .CreateField("TA", dbText, 3)
.Fields.Append fld1
End With
db.TableDefs.Append tbPinas

Wer kann helfen?

Manuela Kulpa
02.01.2002, 09:36
<font face="Verdana" size="2">Hallo Claus!

Anbei ein kleines Beispiel!

vg</font>

<div><link href="http://www.ms-office-forum.net/forum/externals/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Public Sub</span> AddFieldToTable(dbsIn <span class="TOKEN">As</span> DAO.Database, _
sTable <span class="TOKEN">As</span> String, _
sField <span class="TOKEN">As</span> String, _
eDataType <span class="TOKEN">As</span> DAO.DataTypeEnum, _
iLength <span class="TOKEN">As</span> <span class="TOKEN">Integer</span>)


<span class="TOKEN">Dim</span> tdfTable <span class="TOKEN">As</span> DAO.TableDef
<span class="TOKEN">Dim</span> fldField <span class="TOKEN">As</span> DAO.Field

<span class="TOKEN">On Error GoTo</span> HandleErr

<span class="TOKEN">Set</span> tdfTable = dbsIn.TableDefs(sTable)

<span class="TOKEN">If</span> eDataType = dbText <span class="TOKEN">Then</span>
<span class="TOKEN">Set</span> fldField = tdfTable.CreateField(sField, eDataType, iLength)
<span class="TOKEN">Else</span>
<span class="TOKEN">Set</span> fldField = tdfTable.CreateField(sField, eDataType)
<span class="TOKEN">End</span> <span class="TOKEN">If</span>

tdfTable.Fields.Append fldField

ExitHere:
<span class="TOKEN">Exit Sub</span>

HandleErr:
Select Case Err.Number
<span class="TOKEN">Case Else</span>
MsgBox "Fehler " & Err.Number & ": " & _
Err.Description, vbCritical, _
"basAllgemein.AddFieldToTable"
<span class="TOKEN">End</span> Select
<span class="TOKEN">Resume</span> ExitHere
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span><hr>
<span class="TOKEN">Public Sub</span> TestAddFieldToTable()

<span class="TOKEN">Dim</span> dbs <span class="TOKEN">As</span> DAO.Database
<span class="TOKEN">Dim</span> sTable <span class="TOKEN">As</span> <span class="TOKEN">String</span>
<span class="TOKEN">Dim</span> sField <span class="TOKEN">As</span> <span class="TOKEN">String</span>

sTable = "Pinas"
sField = "TA"

<span class="TOKEN">Set</span> dbs = CurrentDb

<span class="TOKEN">Call</span> AddFieldToTable(dbs, sTable, sField, dbText, 3)

<span class="TOKEN">If</span> <span class="TOKEN">Not</span> dbs <span class="TOKEN">Is</span> <span class="TOKEN">Nothing</span> <span class="TOKEN">Then</span> dbs.Close: <span class="TOKEN">Set</span> dbs = <span class="TOKEN">Nothing</span>

<span class="TOKEN">End</span> <span class="TOKEN">Sub</span></pre></div>

Kurt aus Kienitz
02.01.2002, 09:36
Hallo Claus,

Probier es mal so

Sub Dummy()

Dim AktDb As Database
Dim TabDef As TableDef
Dim fld1 As Field

Set AktDb = CurrentDb
Set TabDef = AktDb.TableDefs("Kd1")

With TabDef
Set fld1 = .CreateField("TA", dbText, 3)
.Fields.Append fld1
End With

End Sub