用sql設定access的預設值

2021-04-09 08:46:49 字數 2335 閱讀 4431

如何設定表的某欄位預設值?

簡述:如何定義字段預設值 

問題: 

如何設定表的某欄位預設值?

方法一:

用 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)

end if

fld.properties("unicodecompression") = true

end if

next fld

next tdf

end sub

access中設定字段預設值相關函式

access中設定字段預設值相關函式 datevalue now datepart q now today date years datepart yyyy today 1981 1 days datepart y today 25 ifdatepart m today 12then ifdatep...

用SQL語句查詢Access中某錶是否存在方法

access資料庫雖然功能單一,但的確是乙個小巧精幹的開發夥伴,我在多個專案中與它見面,為了探知access資料庫的本源,今天上網查了些資料。現總結歸納如下 在access資料庫中通過sql語句找到某一表是否存在的確是一件困難的事。幸好,access本身就有一些隱含的資料物件能夠儲存你庫中的資訊,今...

doubleselect標籤的預設值設定

標籤在使用的時候,遇到最多的問題可能就是二級選單沒有顯示資料。這時候,首先你要學會除錯自己的程式,看你的值是否傳過來了。然後再看看自己的標 籤的formname 你的表單的名字 是不是沒有定義。formname對很重要。標籤的預設值的問題 有些時候我們需要給這個級聯選單設定預設選項,怎樣設定呢?這是...