Advertisement

05.06.2008 at 12:18PM PDT, ID: 23380621 | Points: 500
[x]
Attachment Details
How to copy a table's structure, add an autonumber field and make it the primary key.
How can do I the follwoing via code:
- Create a new table (table1) using the sructure of an existing table, add an autonumber field as the first column, and make it the primary key?
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: Milewskp
Question Asked On: 05.06.2008
Participating Experts: 2
Points: 500
Views: 0
Translate:
Loading Advertisement...
05.06.2008 at 01:02PM PDT, ID: 21510529

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 01:07PM PDT, ID: 21510576

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 02:14PM PDT, ID: 21511151

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 02:16PM PDT, ID: 21511160

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 02:23PM PDT, ID: 21511209

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 03:43PM PDT, ID: 21511648

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 04:08PM PDT, ID: 21511745

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.06.2008 at 04:32PM PDT, ID: 21511842

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 07:54PM PDT, ID: 21530154

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.08.2008 at 08:24PM PDT, ID: 21530237

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 04:10AM PDT, ID: 21531646

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 06:31AM PDT, ID: 21532543

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 06:49AM PDT, ID: 21532735

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 03:29PM PDT, ID: 21536773

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 03:34PM PDT, ID: 21536798

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.06.2008 at 01:02PM PDT, ID: 21510529

Rank: Master

Click on table name
Copy
Paste
Structure only

Design table
Add new field > Autonumber
Select the new field > Primary Key (Key icon)
 
05.06.2008 at 01:07PM PDT, ID: 21510576

Rank: Master

TO have the new field the first in the list.
Click on it
then press and drag to top of the list

Or you can click on first field,
Right click and select insert rows
And type your field name and select key icon to make it primary key
 
05.06.2008 at 02:14PM PDT, ID: 21511151

Rank: Genius

You can use data-definition queries for this. If needed:

    DROP TABLE Table2;

Copy structure but no data to Table2:

    SELECT * INTO Table2 FROM Table1 WHERE False;

If needed, alter existing AutoNumber to become a Long:

    ALTER TABLE Table2 ALTER COLUMN ID Integer;

Create new counter as primary key:

    ALTER TABLE Table2
      ADD COLUMN NewID Counter(1,1)
      CONSTRAINT PrimaryKey PRIMARY KEY;

Each can be run using .Execute from CurrentDb or the current connection.

You can do the same thing with DAO, which also handles the Access properties of the fields (format, etc.). Tell me if you need some sample code for that as well.

(°v°)
 
05.06.2008 at 02:16PM PDT, ID: 21511160

Rank: Genius

> as the first column

That would require DAO, I suspect, unless you can tweak the above somehow.

(°v°)
 
05.06.2008 at 02:23PM PDT, ID: 21511209
How would I use DAO to make the Autonumber column the first column?
 
05.06.2008 at 03:43PM PDT, ID: 21511648

Rank: Genius

Oh, hi Milewskp. I hadn't caught your name.

After the data definition queries, you can adjust the Ordinal Position of the fields in the table, accessing them though DAO collections, see below. This moves NewID from last to first.

(°v°)
1:
2:
3:
4:
5:
6:
7:
    Dim fld As DAO.Field
    With CurrentDb
        For Each fld In !Table2.Fields
            fld.OrdinalPosition = fld.OrdinalPosition + 1
        Next
        !Table2!NewID.OrdinalPosition = 0
    End With
Open in New Window
 
05.06.2008 at 04:08PM PDT, ID: 21511745

Rank: Master

"... via code.."
1 Start with table1,
2 copy to table1_2, clear records,
3 add newColumn, set primary
4 (arrange to 1st in list - thanks to harfang for this part)

Private Sub Command3_Click()
    DoCmd.SetWarnings False
    DoCmd.CopyObject , "Table1_2", acTable, "Table1"

    DoCmd.RunSQL "Delete * from Table1_2"

    CurrentDb.Execute "ALTER TABLE Table1_2 ADD COLUMN newColumn Integer"
    CurrentDb.Execute "ALTER TABLE Table1_2 ADD Constraint PKnewColumn Primary Key(newColumn);"
    CurrentDb.TableDefs("Table1_2").Fields("newColumn").OrdinalPosition = 0

DoCmd.SetWarnings True

End Sub
 
05.06.2008 at 04:32PM PDT, ID: 21511842

Rank: Master

CurrentDb.Execute "ALTER TABLE Table1_2 ADD COLUMN newColumn Integer"
===>
CurrentDb.Execute "ALTER TABLE Table1_2 ADD COLUMN newColumn AutoIncrement"
 
05.08.2008 at 07:54PM PDT, ID: 21530154
Hi harfang,
What is the (1,1) in Counter(1,1) for?

Hi hnasr
 <CurrentDb.TableDefs("Table1_2").Fields("newColumn").OrdinalPosition = 0>
Don't you have to bump up the ordinal positons of the other fields first (see the post just before yours).
 
05.08.2008 at 08:24PM PDT, ID: 21530237

Rank: Genius

The (optional) syntax is: COUNTER(<seed>,<increment>). For more information on these parameters, see Access help (pages 'SQL Data Types', 'seed', and 'identity'). Note that AutoIncrement is listed as a synonym for COUNTER (page 'Equivalent ANSI SQL Data Types')

(°v°)
 
05.09.2008 at 04:10AM PDT, ID: 21531646

Rank: Master

Well!
If you change 9th order position to 0, all the rest will be readjusted after 0.
Try it and report back!
This is similar to tab order setting.

Good luck
 
05.09.2008 at 06:31AM PDT, ID: 21532543

Rank: Genius

@hnasr: I find your comment rather disrespectful, when you do not apply this advice to yourself. I did in fact try, before posting, and you are not correct; the ordinal position is *not* similar to tab order setting. In fact, all you had to do was strike F1 on the property name to read:

OrdinalPosition Property
[...]
Two or more Field objects in the same collection can have the same OrdinalPosition property value, in which case they will be ordered alphabetically. [...]

Please: Try it and report back!
(°v°)
 
05.09.2008 at 06:49AM PDT, ID: 21532735
Hi hnasr,
I tried this:
       CurrentDb.TableDefs("[" & TableName & "_Uniques]").Fields(IDFieldName).OrdinalPosition = 0
But it puts IDFieldName in the second position.

WHen I use this:
    With CurrentDb
        For Each fld In .TableDefs("[" & TableName & "_Uniques]").Fields
            fld.OrdinalPosition = fld.OrdinalPosition + 1
        Next
        .TableDefs("[" & TableName & "_Uniques]").Fields(IDFieldName).OrdinalPosition = 0
    End With
It puts IDFieldName in the first position (leftmost column).
 
05.09.2008 at 03:29PM PDT, ID: 21536773

Rank: Master

disrespectful? :(
 
05.09.2008 at 03:34PM PDT, ID: 21536798

Rank: Genius

Ah, well. I was very annoyed at that moment... no hard feelings?
(°v°)
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628