+ -
当前位置:首页 → 问答吧 → 为何老是增加不了字段呢?

为何老是增加不了字段呢?

时间:2010-10-15

来源:互联网

用vba操作Access时使用语句:
cnn.Execute "alter table temp_bom add column id char(25)"
怎么不能增加一个新的字段?

就是想在表里加一个字段ID 自动编号的那种.

作者: moyandukang   发布时间: 2010-10-15

Private Sub sin()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim sql As String
cnn.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.Path & "\mydb.mdb"
sql = "alter table ruku add column id counter"
cnn.Execute sql
Set cnn = Nothing
End Sub

也可把column省略为alter table ruku add id counter

作者: bibisin   发布时间: 2010-10-16

bibisin老师,你好
  我发现单纯用你的代码确实能向Access里已经存在的表增加字段.
但在我程序里面要增加字段的表是用代码生成的,就增加不了字段,不知道为什么:
    jx = UserForm2.ComboBox1.Value
    zjmc = UserForm2.ComboBox2.Value
    cnn.Execute "select * into temp_bom from bom where 机型 ='" & jx & " '  and 组件名称 = '" & zjmc & "' order by 机型,组件名称,单价 desc"
     cnn.Execute "alter table temp_bom add column id counter"
是不是需要生成新表并刷新数据库之后才能增加字段? 请帮助解决.

[ 本帖最后由 moyandukang 于 2010-10-16 13:01 编辑 ]

作者: moyandukang   发布时间: 2010-10-16

热门下载

更多