Let me show how to add a new field into an existing access database using Visual Basic 6.0.Using the tableDef and Filed object you can create new fields. Firstly, you need to create Database and Recordset object, mke sure the DAO access object library.
Dim DB As Database
Dim RS As Recordset
Private Sub Command1_Click()
Call UpdateTableField(“Table1”, “RoomID2”)
End Sub
Private Sub Form_Initialize()
Set DB = OpenDatabase(“F:\Manoj\Code\DB-AUTO-FIELD-ACCES\db3.mdb”)
End Sub
Sub UpdateTableField(tb As String, new_fld As String)
Dim CHECK As Boolean
CHECK = False
Dim tbl As TableDef
Dim fld As Field
Dim strName As String
Set tbl = DB.TableDefs(tb)
For Each fld In tbl.Fields
If fld.Name = new_fld Then
CHECK = True
End If
Next
If CHECK = False Then
Set RS = Nothing
With tbl
Set fld = .CreateField(new_fld, dbText)
fld.DefaultValue = “”
.Fields.Append fld
MsgBox “new Field Created !, check table”
End With
Else
MsgBox “Field already exist !”
End If
You can call as many time you need to create new field in each table in your table.
This is a nice utility and example code block .. How would this be done for an .mdb that has a user level security (.MDW) file mydatabasesecured.mdw file””
LikeLike