Office学院 加入收藏
Office学院 Access Word Excel Powerpoint Wps Access Outlook Tags  
您现在的位置: Office学院 - Access - 用代码更改字段属性

用代码更改字段属性

添加记录: 用代码更改字段属性 类别: Access 发布日期: 2007.03.18
When you manually change a field's properties in a table, Access is really performing several actions at once:

Add a new field with the required property; 
Copy the data from the existing field to the new field; 
Delete the old field.
Therefore, if you want to change a field in code, you will need to do the same. Here is a sample function that uses SQL statements to do this. Obviously, there are properties that can't be set using SQL, so you would need to use DAO instead.
Sub sChangeField(strTableName As String, strFieldName As String, strFieldType As String)
    Dim db As Database
    Dim strSQL As String
    Set db = CurrentDb
'   Add a new field, called TempField, of the correct type in the table
    strSQL = "ALTER TABLE [" & strTableName & "] ADD COLUMN [TempField] " & strFieldType & ";"
    db.Execute strSQL
'   Copy the data from the existing column to the new column
    strSQL = "UPDATE DISTINCTROW [" & strTableName & "] SET [" & strFieldName & "]=[TempField];"
    db.Execute strSQL
'   Delete the existing field from the table
    strSQL = "ALTER TABLE [" & strTableName & "] DROP COLUMN [" & strFieldName & "];"
    db.Execute strSQL
'   Change the new field name back to the existing field name
    db.TableDefs(strTableName).Fields("TempField").Name = strFieldName
    Set db = Nothing
End Sub 

You can then call the sub like:

Call sChangeField("tblName","fldName","TEXT(100)") 

Which will convert the field called fldName in the table tblName to a text field with a maximum length of 100 characters. 

顶一下
上一篇:[绝招]Access人士必读
下一篇:用代码创建DSN
Tags: Access pda

相关信息
  • [绝招]Access人士必读
  • 在查询与窗体列表中控制小数位
  • [LWWVB教程]设计表的精义(第2课)
  • 如何用Access开发局域网软件(5)
  • 如何用Access开发局域网软件(4)
  • 如何用Access开发局域网软件(3)
  • 我的一个编程的经验
  • 如何用Access开发局域网软件(2)
  • 如何用Access开发局域网软件(1)
  • 我的ACCESS处女作,销售单统计
  • 用Access来进行数据分析随笔
  • 专栏作家-李伟文[lwwvb]
  • 本站聊天室开通
  • 一点感悟:关于与如何与用户交流
  • MVP-张志[竹笛]
  • 用代码创建DSN
  • 获得链接表路径
  • 使用Access时必须避免的特殊字符
  • 使用 SetOption 来禁用警告消息
  • 如何以编程方式更新数据访问页链接
  • 绑定到存储过程或函数的窗体或报表没有应用WHERE条件
  • 从Excel导入或链接数据后Excel工作表的列格式丢失或更改
  • Access2003如何解析 VBA引用
  • 如何在Access数据库相关表中编辑记录
  • [LWWVB教程]设计表的精义(第4课)
  • 设计“编码”的几个经验
  • 根据企业特点定义月度范围
  • 关于我们
  • [FAQ]去掉XP窗体右上角的帮助框
  • [FAQ]如何查看VBA源程序














  • 生日密码
    血型分析
    生肖分析



    制作单位 Office学院 © 版权所有


    联系我们 关于我们 友情链接 站点地图 免责声明


    Office学院,致力于打造专业的OFFICE应用交流平台。