access vba表字段_使用VBA向MS Access表添加字段

在Access中向现有表添加计算字段时,有两种方法:通过TableDef.CreateField和Fields.Append,或者使用DDL的ALTER TABLE ADD COLUMN语句。作者尝试了第一种方法但遇到了3211错误,因为无法锁定表。问题可能源于在尝试添加字段的表单已经访问过表中的字段。文章提供了使用DDL创建字段的示例代码,并讨论了两种方法的优缺点。
摘要由CSDN通过智能技术生成

I need to add a calculated field to an existing table. I am aware of two ways to do this and I'm wondering if anyone has any input on which is best and how to make them work:

Using TableDef.CreateField, then TableDef.Fields.Append

Using a DDL Alter Table ADD COLUMN statement

I tried using the first method, but I keep getting a 3211 error because Access could not lock the table. I don't have the table open. However, I am calling CreateField from a form that has accessed which fields currently exist in the table.

Here's the code for calling CreateField:

`

Public Sub AddFieldToTable(strTable As String, strField As String, nFieldType As Integer)

Dim db As DAO.Database

Dim tdf As DAO.TableDef

Dim fld As DAO.Field

On Error GoTo ErrorHandler

Set db = CurrentDb

Set tdf = db.TableDefs(

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值