0

I go to Create tab and hit Query Design and then SQL view and enter the following. But it doesn't run. I've tried googling and I follow those rules and I still get errors. I've change the statement every way I know and it still doesn't run. What am I doing wrong? Note: Last Name has to be VARCHAR and Commission and Rate both have to be NUMBER.

I redid it with some of the suggests and changed it to this.

CREATE TABLE SALES_REP
(REP_NUM CHAR(2) PRIMARY KEY,
LAST_NAME VARCHAR(15),
FIRST_NAME CHAR(15),
STREET CHAR(15),
CITY CHAR(15),
STATE CHAR(2),
POSTAL_CODE CHAR(5),
COMMISSION NUMBER(7,2),
RATE NUMBER(3,2) );

Syntax error in CREATE TABLE STATEMENT

5
  • Which errors are you getting exactly? Edit your question with that please. Commented Sep 3, 2015 at 21:43
  • 1
    remove the space in your table name. Change it to Sales_Rep Commented Sep 3, 2015 at 21:43
  • 1
    POSTAL_CODE needs a data type. Commented Sep 3, 2015 at 21:44
  • Surround your table name in brackets: [SALES REP]. Table names are allowed to have spaces, but only if encased in brackets. Commented Sep 3, 2015 at 21:44
  • 1
    Which RDBMS are you targetting? An Access back-end, MySql, or MSSQL? You can't possibly be working in all 3. Commented Sep 3, 2015 at 21:46

2 Answers 2

1

In MS Access, if you want DDL to specify Scale and Precision for a numeric field, use DECIMAL as the field type.

CREATE TABLE SALES_REP
(REP_NUM CHAR(2) PRIMARY KEY,
LAST_NAME VARCHAR(15),
FIRST_NAME CHAR(15),
STREET CHAR(15),
CITY CHAR(15),
STATE CHAR(2),
POSTAL_CODE CHAR(5),
COMMISSION DECIMAL(7,2),
[RATE] DECIMAL(3,2));

Notice I bracketed the field name [RATE] so the db engine won't confuse it with the VBA Rate() function.

Beware in order for DECIMAL to be supported you must execute the statement from ADO/OleDb. I executed it withCurrentProject.Connection.Execute, which is an ADO method, like this ...

Dim strDdl As String
strDdl = "CREATE TABLE SALES_REP" & vbCrLf & _
    "(REP_NUM CHAR(2) PRIMARY KEY," & vbCrLf & _
    "LAST_NAME VARCHAR(15)," & vbCrLf & _
    "FIRST_NAME CHAR(15)," & vbCrLf & _
    "STREET CHAR(15)," & vbCrLf & _
    "CITY CHAR(15)," & vbCrLf & _
    "STATE CHAR(2)," & vbCrLf & _
    "POSTAL_CODE CHAR(5)," & vbCrLf & _
    "COMMISSION DECIMAL(7,2)," & vbCrLf & _
    "[RATE] DECIMAL(3,2));"
Debug.Print strDdl
CurrentProject.Connection.Execute strDdl

Executing the same statement from DAO (CurrentDb.Execute strDdl or from the query designer) will trigger a syntax error.

Sign up to request clarification or add additional context in comments.

Comments

0

Use double instead of number:

CREATE TABLE SALES_REP
(REP_NUM CHAR(2) PRIMARY KEY,
LAST_NAME VARCHAR(15),
FIRST_NAME CHAR(15),
STREET CHAR(15),
CITY CHAR(15),
STATE CHAR(2),
POSTAL_CODE VARCHAR(5),
COMMISSION DOUBLE,
RATE DOUBLE );

2 Comments

Why will Number not work? It's working due to the change to DOUBLE, but the book wants it listed as NUMBER?
It isn't a valid Ms access datatype

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.