Use SQL to set the default value for access _ database related

Source: Internet
Author: User
Tags ole
Briefly:
How to define a field default value

Problem:
How do I set a field default value for a table?

Method One:

Using JET SQL to complete
Alter TABLE TableName ALTER COLUMN FIELDNAME TEXT default value


Please note that the above statements are ADODB. CONNECTION. Execute methods, and you can create a query that is not saved or run directly with the above code, and you get an error message from ACCESS.


Method Two:

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 ' must be filled in
' Required cannot be set with the above code, the error message is:
' Multi-step OLE DB operation generated an error. If possible, check each OLE DB status value. No work is done.
' It can now be set with the following statement:
' Currentdb.tabledefs ("Ke_hu"). Fields ("Dw_name"). Properties ("Required") = False
. Properties ("Jet oledb:allow Zero Length") = True ' allows nulls
. Properties ("default") = "silently recognize and recognize" ' Default value
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


Some references to "multi-Step Errors":

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

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.