Use SQL to set the default value of access

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.