威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
ASP教程>> ASP基础 应用技巧 数据库相关 ASP类 存储过程 FSO专栏 ASP其他
当前位置:首页 > ASP教程 > 数据库相关
上一节 下一节
 用sql设置access的默认值
简述:
如何定义字段默认值 

问题: 
如何设定表的某字段默认值?

方法一:

用 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
申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!
上一节 下一节
相关教程  
其他教程  
ASP基础
应用技巧
数据库相关
ASP类
存储过程
FSO专栏
ASP其他

违法和不良信息举报中心】邮箱:254677821@qq.com
Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821