Solved

SQL Syntax required for ALTER TABLE

Posted on 2002-07-22
8
1,546 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

734 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