设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1053|回复: 1
打印 上一主题 下一主题

[ADO/DAO] 如何使用DAO或者其他方法更改已有字段的属性?

[复制链接]
跳转到指定楼层
1#
发表于 2002-11-25 16:34:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我在用ACCESS处理BAAN数据到用友里去,现在有个问题,我用DAO增加字段同时可以设定字段相关数据类别等信息,但原来已经有的字段我没有找到方法通过程序修改其字段信息:数据类别、默认值、长度等。请高手指点。以下为DAO帮助说明的信息。
In Visual Basic, you can use the ADO Type property to set a field's data type before appending it to the Fields collection.

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2002-11-25 17:52:00 | 只看该作者
http://ourworld.compuserve.com/homepages/attac-cg/AtblTip.htm#FORMAT
Sub: Setting A Field's Format Via Code Either During Table Creation or Afterwards

Detail: When you are creating a table using code you may want to set a field's format or number of decimal places.
Alternately if you run a make table query using already formated fields as an input, you will find that the new table does not carry over the formatting of your input fields. Therefore in each situation, you need to set the format for the field.

The format and decimal places properties of a field do not exist until they are created, so if you query a field's "format" property before it is created, you'll get an error saying there is no such property. So here's some code which creates a simple table, and then sets the format and decimal places properties for a couple of fields. You can strip out the code for the format section to create a new function for setting the format for a table after running a make table query.

(Note: This code does not work using ADO in Access 2000, in fact there is no similar ability to set decimal places or formatting of fields if you use ADO as the data access method of your db in Access 2000. You must create a temporary reference to or use DAO as the data access method in Access 2000 to support this capability.)

Function acg_CreateTable(strTable As String) As Integer
'-------------------------
'Purpose:  Creates A new table and sets field format
'Accepts:  strTable, the name of the new table
'Returns:  True (-1) on success, False on failure
'-------------------------
On Error GoTo ErrCT

Dim TDB As Database
Dim fld1 As Field, fld2 As Field, fld3 As Field
Dim fFormat2 As Property, fFormat3 As Property
Dim idxTbl As Index
Dim idxFld As Field
Dim Newtbl As TableDef
Dim Newtbl2 As TableDef

acg_CreateTable = True

'First Create the table

Set TDB = CurrentDb()
Set Newtbl = TDB.CreateTableDef(strTable)
Set fld1 = Newtbl.CreateField("MyStringField", dbText, 75)
Newtbl.Fields.Append fld1
Set fld2 = Newtbl.CreateField("MyNumberField", dbDouble)
Newtbl.Fields.Append fld2
Set fld3 = Newtbl.CreateField("MyDateTimeField", dbDate)
Newtbl.Fields.Append fld3
TDB.TableDefs.Append Newtbl

'Create an index for our table.  Need to use a new tabledef
'object for the table or it doesn't work

Set Newtbl2 = TDB.TableDefs(strTable)
Set idxTbl = Newtbl2.CreateIndex("rimaryKey")
idxTbl.Primary = -1
idxTbl.Unique = -1
Set idxFld = idxTbl.CreateField("MyStringField")
idxTbl.Fields.Append idxFld
Newtbl2.Indexes.Append idxTbl

'Format the single field to have two decimal places
'and the datetime field to be a medium time.
'Note that decimal places has no space in the name

Set fld2 = Newtbl2.Fields("MyNumberField")
Set fFormat2 = fld2.CreateProperty("Format", dbText, "Fixed")
fld2.Properties.Append fFormat2
Set fFormat2 = fld2.CreateProperty("DecimalPlaces", dbByte, 2)
fld2.Properties.Append fFormat2
Set fld3 = Newtbl2.Fields("MyDateTimeField")
Set fFormat3 = fld3.CreateProperty("Format", dbText, "Medium Time")
fld3.Properties.Append fFormat3

TDB.Close
   
ExitCT:
    Exit Function
ErrCT:
    If Err <> 91 Then TDB.Close
    acg_CreateTable = False
    Resume ExitCT
End Function

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-29 20:01 , Processed in 0.119287 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表