当前位置: 首页 > 图文教程 > 网络编程 > ASP > 用sql设置access的默认值

ASP
ASP下经常用的字符串等函数参考资料
asp下连接数据库 ASP链接数据库字符串大全总结
asp内置对象 ObjectContext 事务管理 详解
asp 内置对象 Application 详解
ASP中 SQL语句 使用方法
ASP 中 Split 函数的实例分析
ASP 中 DateDiff 函数详解 主要实现两日期加减操作
asp 存贮过程 (SQL版asp调用存储过程)
利用ASP实现在线生成电话图片效果脚本附演示
使用ASP记录在线用户的数量的代码
关于ASP生成伪参数技巧 简洁实用的伪(僞)参数
asp 关键词字符串分割如何实现方法
用ASP编写的加密和解密类
ASP之处理用Javascript动态添加的表单元素数据的代码
asp下最常用的19个基本技巧
一些Asp技巧和实用解决方法
asp实现一个统计当前在线用户的解决方案
asp下的一个很简单的验证码程序
asp又一个分页的代码例子
asp实现防止从外部提交数据的三种方法

ASP 中的 用sql设置access的默认值


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 221 ::
收藏到网摘: n/a

简述:
如何定义字段默认值
问题:
如何设定表的某字段默认值?
方法一:
用 JET SQL 来完成
ALTER TABLE TABLENAME ALTER COLUMN FIELDNAME TEXT(40) DEFAULT 默认值

请注意,上述语句要用 ADODB.CONNECTION.EXECUTE 等方法来执行,直接用上述代码建立一个查询无法保存或者运行,会得到 ACCESS 的错误提示。

方法二:
ADOX 可以。

Function ChengTableFieldPro_ADO()
Dim MyTableName As String
Dim MyFieldName As String
Dim GetFieldDesc_ADO
Dim GetFieldDescription
MyTableName = "ke_hu"
MyFieldName = "dw_name"
Dim MyDB As New ADOX.Catalog
Dim MyTable As ADOX.Table
Dim MyField As ADOX.Column
On Error GoTo Err_GetFieldDescription
MyDB.ActiveConnection = CurrentProject.Connection
Set MyTable = MyDB.Tables(MyTableName)
GetFieldDesc_ADO = MyTable.Columns(MyFieldName).Properties("Description")

Dim pro As ADODB.Property
For Each pro In MyTable.Columns(MyFieldName).Properties
Debug.Print pro.Name & " : " & pro.Value & " ---- type : " & pro.Type
Next
With MyTable.Columns(MyFieldName)
'.Properties("nullable") = True '必填
'必填无法用上述代码设置,出错提示为:
'多步 OLE DB 操作产生错误。如果可能,请检查每个 OLE DB 状态值。没有工作被完成。
'目前可以用以下语句设置:
'CurrentDb.TableDefs("ke_hu").Fields("DW_NAME").Properties("Required") = False
.Properties("Jet OLEDB:Allow Zero Length") = True '允许空
.Properties("default") = "默默默默认认认认" '默认值
End With
Set MyDB = Nothing
Bye_GetFieldDescription:
Exit Function
Err_GetFieldDescription:
Beep
Debug.Print Err.Description
MsgBox Err.Description, vbExclamation
GetFieldDescription = Null
Resume Bye_GetFieldDescription
End Function

关于“多步错误”的一些参考:
Sub ChangeUnicode()
Dim tdf As TableDef
Dim fld As Field
Dim db As Database
Dim pro As Property
Set db = CurrentDb
For Each tdf In db.TableDefs
For Each fld In tdf.Fields
If fld.Type = dbText Then
If DBEngine.Errors(0).Number = 3270 Then
Set pro = fld.CreateProperty("UnicodeCompression", 1, 0)
fld.Properties.Append p
End If
fld.Properties("UnicodeCompression") = True
End If
Next fld
Next tdf
End Sub