Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How create autonumber key field with a make table query

Posted on 2013-10-22
6
Medium Priority
?
15,825 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 49

Expert Comment

by:Dale Fye
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 61

Accepted Solution

by:
mbizup earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

783 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