Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Syntax required for ALTER TABLE

Posted on 2002-07-22
8
Medium Priority
?
1,555 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 150 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

Technology Partners: 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!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

604 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