请问怎么向一个ACCESS表里面增加字段?

laoniu96 2004-10-07 09:52:59
请问怎么向一个ACCESS表里面增加字段?
...全文
62 点赞 收藏 3
写回复
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
RUKYO 2004-10-07
'也可以引用ADOX对象,例如:
'工程->引用Microsoft ADO Ext. 2.x for DDL and Security
Private Sub Command1_Click()
Dim mTbl As New Table
Dim mIdx As New ADOX.Index
Dim mCat As New ADOX.Catalog


mCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb" & ";Persist Security Info=False"

mCat.Tables("OneTable").Columns.Append "addFieldA", adInteger '动态添加一个整型字段
mCat.Tables("OneTable").Columns.Append "addFieldB", adVarWChar '动态添加一个文本型字段
End Sub
回复
Zerosir 2004-10-07
以下是我的方法(当然要先设置连接m_cnADODB,此步略):
'//添加新字段通用函数
Public Function FieldAddNew(TableName As String, ColumnName As String, ColumnType As String) As Boolean
On Error GoTo errFieldAddNew
Dim strTempSQL As String

strTempSQL = "ALTER TABLE " & TableName & " ADD COLUMN " & ColumnName & " " & ColumnType & ""
m_cnADODB.Execute (strTempSQL)

FieldAddNew = True
Exit Function

errFieldAddNew:
MsgBox "添加新字段时出错。" & Err.Description, vbExclamation
FieldAddNew = False
Err.Clear
Exit Function

End Function
回复
programfish 2004-10-07
Dim Wrk As Workspace
Set Wrk = DBEngine.Workspaces(0)
Set dbs = Wrk.CreateDatabase(App.Path & "\" & DatabaseName & TableName & "Outputtemp.mdb", dbLangChineseSimplified)
Set tdfNew = dbs.CreateTableDef(TableName)

tdfNew.Fields.Append tdfNew.CreateField(Col1, dbText)
tdfNew.Fields.Append tdfNew.CreateField(Col2, dbText)
tdfNew.Fields.Append tdfNew.CreateField(Col3, dbText)
tdfNew.Fields.Append tdfNew.CreateField(Col4, dbText)
dbs.TableDefs.Append tdfNew
回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告