Brief description:
How to define the default Field Value
Problem:
How to set the default value of a field in a table?
Method 1:
Use jet SQL to complete
Alter table tablename alter column fieldname text (40) default Default Value
Note that the preceding statement must be executed using methods such as ADODB. Connection. Execute.CodeIf a query cannot be saved or run, an access error is returned.
Method 2:
ADOX can.
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' required
'The above Code cannot be set as required. The error prompt is:
'Multiple-step ole db operation produces an error. If possible, check the status value of each ole db. No work is completed.
'Currently, you can use the following statements to set the parameters:
'Currentdb. tabledefs ("ke_hu"). Fields ("dw_name"). properties ("required") = false
. Properties ("jet oledb: Allow zero length") = true 'allow null
. Properties ("default") = "Silently recognizing" 'Default Value
End
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
Some references about "multi-step error:
Sub changeunicode ()
Dim TDF as tabledef
Dim stored as Field
Dim dB as database
Dim pro as property
Set DB = currentdb
For each TDF in db. tabledefs
For each records in TDF. Fields
If response. type = dbtext then
If dbengine. errors (0). Number = 3270 then
Set pro = cipher. createproperty ("unicodecompression", 1, 0)
Consumer. properties. append P
End if
Criteria. properties ("unicodecompression") = true
End if
Next iteration
Next TDF
End sub