Solved

SQL Syntax required for ALTER TABLE

Posted on 2002-07-22
8
1,545 Views
Last Modified: 2008-02-26
Hi Experts,

I need to change existing table using ALTER TABLE query.
I want to add few new fields to the table and I got stucked on onr type, NUMERIC.

The field I want to create is has following parameters:
Data Type:      NUMBER
Field Size:     SINGLE
Format:         Fixed
Decimal Places: 2

I have tried all known to me syntaxes without success and MS Access help is useless.
0
Comment
Question by:graga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 4

Expert Comment

by:zuijdhoek
ID: 7171129
Graga,

You just can't specify field properties using some SQL-statement. Assuming you're using DAO you can add fields to a table and specify it's properties. This is an example of how to do it:

Sub AlterTable()
On Error GoTo ErrHandling
    Dim tdf As TableDef
    Dim db As Database
    Dim fld As Field

    Set db = CurrentDb
    Set tdf = db.TableDefs("tblTest")
 
    Set fld = tdf.CreateField("MyField", dbSingle)
    tdf.Fields.Append fld 'append field first!

    'these properties don't belong to the default collection
    'of field-properties so have to be created first
    fld.Properties.Append fld.CreateProperty("Format", dbText, "Fixed")
    fld.Properties.Append fld.CreateProperty("DecimalPlaces", dbInteger, 2)

   
ExitHere:
    Set fld = Nothing
    Set tdf = Nothing
    Set db = Nothing
   
    Exit Sub
   
ErrHandling:
    Select Case Err.Number
    Case Else
        MsgBox Err.Description, vbExclamation, _
         "Error " & Err.Number
    End Select
   
    Resume ExitHere
   
End Sub

Hopefully this makes sense to you,

Mark
0
 
LVL 5

Expert Comment

by:KMAN
ID: 7171801
"You just can't specify field properties using some SQL-statement"

Huh?  SQL has been around longer than DAO and you can.  

ALTER TABLE myTable ADD COLUMN New_Field SINGLE



FROM HELP FILE:
---------------------------------------------------------
Syntax
ALTER TABLE table {ADD {COLUMN field type[(size)] [NOT NULL]     [CONSTRAINT index] |
    ALTER COLUMN field type[(size)] |
    CONSTRAINT multifieldindex} |
    DROP {COLUMN field I CONSTRAINT indexname} }

The ALTER TABLE statement has these parts:

Part Description
table The name of the table to be altered.
field The name of the field to be added to or deleted from table. Or, the name of the field to be altered in table.
type The data type of field.
size The field size in characters (Text and Binary fields only).
index The index for field. For more information on how to construct this index see CONSTRAINT Clause.  
multifieldindex The definition of a multiple-field index to be added to table. For more information on how to construct this index see CONSTRAINT Clause.
indexname The name of the multiple-field index to be removed.


Remarks
Using the ALTER TABLE statement you can alter an existing table in several ways. You can:

Use ADD COLUMN to add a new field to the table. You specify the field name, data type, and (for Text and Binary fields) an optional size. For example, the following statement adds a 25-character Text field called Notes to the Employees table:
ALTER TABLE Employees ADD COLUMN Notes TEXT(25)


You can also define an index on that field. For more information on single-field indexes see CONSTRAINT Clause.

If you specify NOT NULL for a field then new records are required to have valid data in that field.

Use ALTER COLUMN to change the data type of an existing field. You specify the field name, the new data type, and an optional size for Text and Binary fields. For example, the following statement changes the data type of a field in the Employees table called ZipCode (originally defined as Integer) to a 10-character Text field:
ALTER TABLE Employees ALTER COLUMN ZipCode TEXT(10)


Use ADD CONSTRAINT to add a multiple-field index. For more information on multiple-field indexes see CONSTRAINT Clause.
Use DROP COLUMN to delete a field. You specify only the name of the field.
Use DROP CONSTRAINT to delete a multiple-field index. You specify only the index name following the CONSTRAINT reserved word.
--------------------------------------------------------------------------------

Notes

You cannot add or delete more than one field or index at a time.
You can use the CREATE INDEX statement to add a single- or multiple-field index to a table, and you can use ALTER TABLE or the DROP statement to delete an index created with ALTER TABLE or CREATE INDEX.
You can use NOT NULL on a single field or within a named CONSTRAINT clause that applies to either a single field or to a multiple-field named CONSTRAINT. However, you can apply the NOT NULL restriction only once to a field. Attempting to apply this restriction more than once restuls in a run-time error.
---------------------------------------------------------


GL, K
0
 
LVL 11

Expert Comment

by:joekendall
ID: 7171838
This line will not change the Decimal Places:

fld.Properties.Append fld.CreateProperty("DecimalPlaces", dbInteger, 2)

zuijdhoek:

Did you get it to create the Decimal Places?

Thanks!

Joe


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:KMAN
ID: 7171903
If you set the field type to single, you can't specify the number of decimals for storage... only for display of data (the format).

If you want the ability to set decimals for storage, then you need to use the Decimal datatype, new in Access 9.0 (2000).  Its uses are limited in that version though, and you may not be able to do certain things with the field, like you can with a single.

K
0
 

Author Comment

by:graga
ID: 7173088
I agree with KMAN,

Looks like SINGLE, as well as NUMERIC don't allow to specity decimals.

KMAN, do you have the syntax for adding DECIMAL? I'm running 2000 but can't get it to work.

The following fails:

ALTER TABLE MyTable ADD NewField DECIMAL
0
 
LVL 5

Expert Comment

by:KMAN
ID: 7174145
Have you tried something like?:
ALTER TABLE MyTable ADD NewField DECIMAL(16,2)

I'll have a go over here on the syntax for decimal...
0
 
LVL 5

Accepted Solution

by:
KMAN earned 50 total points
ID: 7174159
I don't see a way to go decimal with ALTER TABLE.

If you go single or double, you can just format the output at the field or control levels to look like you want.

Decimal is new to Access and functionality associatied with the field type Decimal is definitely limited, as opposed to the other numeric types that have been around for prior versions.

GL, K
0
 

Author Comment

by:graga
ID: 7175772
Yep, looks like there is no way to do it.
I think I need to use CURRENCY to at least get close to what I want.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question