Solved

How create autonumber key field with a make table query

Posted on 2013-10-22
6
11,593 Views
Last Modified: 2013-10-22
I have a make table query which when run needs to create a autonumber key field in the resulting table.  The caption for this field needs to be "Item Number".  How can I make this happen?
0
Comment
Question by:SteveL13
  • 3
  • 2
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39590975
As far as I know, you cant do that directly.  You'll have to alter the table to add the autonumber field after running the make-table query.

In VBA, something like this:

Dim strSQL as string

' Code to run your make table query goes here
'
' Then ALTER the table...
strSQL = "ALTER TABLE YourTableName ADD ItemNumber AUTOINCREMENT(1, 1)"
CurrentDB.execute strSQL

Open in new window

0
 

Author Comment

by:SteveL13
ID: 39590996
ItemNumber has to have a space between the words.  

Should this then read:

strSQL = "ALTER TABLE YourTableName ADD "Item Number" AUTOINCREMENT(1, 1)"


?
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39591000
If you create a table (tbl_Auto) with only a single field (ID) which is set to autonumber and no records, then you can create a query to build your destination table something like:

SELECT tbl_Auto.ID, yourTable.*
INTO yourNewTable
FROM tbl_Auto, yourTable

Since there are no records in tbl_Auto, there will be no records in this new table, but you can then use an append query to append your data to that table:

INSERT INTO yourNewTable (Field1, Field2, Field3)
SELECT Field1, Field2, Field3
FROM yourTable

either way, mbizup's or this one, will require multiple steps.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39591006
Try square brackets:

strSQL = "ALTER TABLE YourTableName ADD "[Item Number]" AUTOINCREMENT(1, 1)"
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39591017
--->>> Try square brackets:

But as an aside, I'd recommend creating the field with no space in the name (better, standard naming conventions), and querying the resulting table as needed for displaying the name with a space.

Ie this to modify the table:

strSQL = "ALTER TABLE YourTableName ADD ItemNumber AUTOINCREMENT(1, 1)"

and this to retrieve the data for display:

SELECT *, ItemNumber AS [Item Number]
FROM YourTable

again, just standard naming conventions.
0
 

Author Comment

by:SteveL13
ID: 39591030
I removed the quote marks from this line and it works fine.  Thanks.

strSQL = "ALTER TABLE YourTableName ADD "[Item Number]" AUTOINCREMENT(1, 1)"

new line"

strSQL = "ALTER TABLE YourTableName ADD [Item Number] AUTOINCREMENT(1, 1)"
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Access VBA Form - Filter Multiple and/or 7 36
Dcount unique 6 24
Dateadd 3 22
Why is my report opening in "MAX" view? 7 10
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now