Hi all
I need to write some code to add fields to a table in a remote database, i have come accross the tabledef command, but i do not want to create a new table and add data, which i cannot get around. This is the code i am using and i get error table already exists, which is correct, but how can i just add new fields.
any help very much apprechiated.
Private Sub Command1_Click()
Dim dbsLSG As Database
Dim tdfNew As TableDef
Dim fldLoop As Field
Dim prpLoop As Property
Dim strDataPath As String
strDataPath = "C:\LSG Enfield\LSG2000-Enfield-data.mdb"
Set dbsLSG = OpenDatabase(strDataPath)
Set tdfNew = dbsLSG.CreateTableDef("members")
' Create and append new Field objects for the new
' TableDef object.
With tdfNew
' The CreateField method will set a default Size
' for a new Field object if one is not specified.
.Fields.Append .CreateField("TextField", dbText)
.Fields.Append .CreateField("IntegerField", dbInteger)
.Fields.Append .CreateField("DateField", dbDate)
End With
dbsLSG.TableDefs.Append tdfNew
Debug.Print "Properties of new Fields in " & tdfNew.Name
' Enumerate Fields collection to show the properties of
' the new Field objects.
For Each fldLoop In tdfNew.Fields
Debug.Print " " & fldLoop.Name
For Each prpLoop In fldLoop.Properties
' Properties that are invalid in the context of
' TableDefs will trigger an error if an attempt
' is made to read their values.
On Error Resume Next
Debug.Print " " & prpLoop.Name & " - " & _
IIf(prpLoop = "", "[empty]", prpLoop)
On Error GoTo 0
Next prpLoop
Next fldLoop
' Delete new TableDef because this is a demonstration.
'dbsLSG.TableDefs.Delete tdfNew.Name
dbsLSG.Close
End Sub
I need to write some code to add fields to a table in a remote database, i have come accross the tabledef command, but i do not want to create a new table and add data, which i cannot get around. This is the code i am using and i get error table already exists, which is correct, but how can i just add new fields.
any help very much apprechiated.
Private Sub Command1_Click()
Dim dbsLSG As Database
Dim tdfNew As TableDef
Dim fldLoop As Field
Dim prpLoop As Property
Dim strDataPath As String
strDataPath = "C:\LSG Enfield\LSG2000-Enfield-data.mdb"
Set dbsLSG = OpenDatabase(strDataPath)
Set tdfNew = dbsLSG.CreateTableDef("members")
' Create and append new Field objects for the new
' TableDef object.
With tdfNew
' The CreateField method will set a default Size
' for a new Field object if one is not specified.
.Fields.Append .CreateField("TextField", dbText)
.Fields.Append .CreateField("IntegerField", dbInteger)
.Fields.Append .CreateField("DateField", dbDate)
End With
dbsLSG.TableDefs.Append tdfNew
Debug.Print "Properties of new Fields in " & tdfNew.Name
' Enumerate Fields collection to show the properties of
' the new Field objects.
For Each fldLoop In tdfNew.Fields
Debug.Print " " & fldLoop.Name
For Each prpLoop In fldLoop.Properties
' Properties that are invalid in the context of
' TableDefs will trigger an error if an attempt
' is made to read their values.
On Error Resume Next
Debug.Print " " & prpLoop.Name & " - " & _
IIf(prpLoop = "", "[empty]", prpLoop)
On Error GoTo 0
Next prpLoop
Next fldLoop
' Delete new TableDef because this is a demonstration.
'dbsLSG.TableDefs.Delete tdfNew.Name
dbsLSG.Close
End Sub