Solved

How create autonumber key field with a make table query

Posted on 2013-10-22
6
13,596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

738 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