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?
jc31415Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jc31415Author Commented:
Edited text of question.
0
jc31415Author Commented:
Edited text of question.
0
clockwatcherCommented:
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);"


0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

clockwatcherCommented:
Whoops!  I didn't notice you didn't have a space in there.  Without the space:

 dbs.Execute "CREATE TABLE " & TableName & " (TCName TEXT CONSTRAINT PrimaryKey PRIMARY KEY,vendor TEXT);"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JimMorganCommented:
It is my understanding that if you don't create a primary key, the first field of the table defaults as the primary key.
0
jc31415Author Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.