Share via


How to: Create Default Field Values

You can specify default values that fields in database tables contain when adding new records, regardless of how the data is entered. These default values remain until they are replaced with new values.

Note

You can specify default values for fields of any data type except General fields.

You can also view the default value for the field.

To specify a default value for a field

  1. Open the database containing the table.

  2. Open the table in the Table Designer.

  3. On the Fields tab, select the field that you want.

  4. In the Default value box in the Field validation area, type the default value you want for the field.

    Tip

    Enclose text in quotation marks ("") for character fields.

  5. Click OK.

For more information, see Fields Tab, Table Designer.

To specify a default value for a field programmatically

  • When creating the table using the SQL CREATE TABLE command, include the DEFAULT clause.

-OR-

  • To edit an existing table, open the table with the USE command and then use the SQL ALTER TABLE command with the DEFAULT clause.

For more information, see CREATE TABLE - SQL Command and ALTER TABLE - SQL Command.

For example, suppose you want to your application to limit the amount of merchandise a new customer can order until you can complete a credit check and determine the amount of credit you want to extend to that customer. The following code creates a customer table with a maximum order amount field with a default value of 1000:

CREATE TABLE Customer (Cust_ID C(6), Company C(40), Contact C(30), ;
   MaxOrdAmt Y(4) DEFAULT 1000)

If the table exists, you can add a default value for the field:

ALTER TABLE Customer ALTER COLUMN MaxOrdAmt SET DEFAULT 1000

To view the default value for a field in a database table

  • Use the DBGETPROP( ) function to retrieve the value of the DefaultValue field property.

For more information, see DBGETPROP( ) Function.

See Also

Tasks

How to: Open Databases

How to: Edit Table Structure

How to: Set Autoincrementing Field Values

Concepts

Field Creation

Other Resources

Working with Fields

Working with Tables (Visual FoxPro)