Link to home
Start Free TrialLog in
Avatar of jc31415
jc31415

asked on

Problem with CREATE TABLE

In a form I create a table. The code lines follow:
 dbs.Execute "CREATE TABLE " & TableName & " (TCName TEXT CONSTRAINT c PRIMARY KEY,vendor TEXT);"
 dbs.TableDefs.Refresh
The table is created correctly and I fill it with data. Then I try to access a record with the following code:
 Set dbs = CurrentDb()
 Set rst = dbs.OpenRecordset(TableName)
 rst.Index = "PrimaryKey"

I get an error: "Primary Key isn't an index in this table.

When I open the table and look at the definition for TCName it IS a primary key. If I turn the 'Key' off and then back on and rerun the form, everything is ok.
What is going on?
Avatar of jc31415
jc31415

ASKER

Edited text of question.
Avatar of jc31415

ASKER

Edited text of question.
You're using CONSTRAINT c PRIMARY KEY which is creating your primary key, but naming it 'c'.

Try:

 Set dbs = CurrentDb()
 Set rst = dbs.OpenRecordset(TableName)
 rst.Index = "c"

Or try changing your create table to this:

 dbs.Execute "CREATE TABLE " & TableName & " (TCName TEXT CONSTRAINT [Primary Key] PRIMARY KEY,vendor TEXT);"


ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is my understanding that if you don't create a primary key, the first field of the table defaults as the primary key.
Avatar of jc31415

ASKER

Thanks! In the description of the CONSTRAINT clause I didn't see anything about the meaning or use of the name field. It seemed to be completely arbitrary.