+ Reply to Thread
Results 1 to 6 of 6

Access VBA to add a primary key (first field) after importing to access

  1. #1
    Registered User
    Join Date
    08-31-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    87

    Access VBA to add a primary key (first field) after importing to access

    Hi,

    I need help in modifying my access vba code. I currently have a sheet that i always import to access. the excel sheet has 4 columns (account id, account name, account period, account period date).

    when importing, i would also like access to automatically create an additional field in front of column A (account id) for primary key (auto number).

    can anyone help me with this?

    Here's the code that I have now:

    Please Login or Register  to view this content.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,879

    Re: Access VBA to add a primary key (first field) after importing to access

    Look here
    http://www.mrexcel.com/forum/microso...imary-key.html
    Alan
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    08-31-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    87

    Re: Access VBA to add a primary key (first field) after importing to access

    I am using this code right now. So just to confirm, this code is to add primary key field to a table in access right? so i'm new in coding so i wanted to confirm with you that this code (below) basically creates a new field called "ID" as the primary key and it's set to autonumber as well right?

    Is there any way to move it to the first column though? When I run the code, it's placed at the last column. I want it to be in the first column though. Thanks

    Please Login or Register  to view this content.
    Quote Originally Posted by alansidman View Post

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,879

    Re: Access VBA to add a primary key (first field) after importing to access

    What happens when you run the code? Did it work? Did it error out?
    Posting untested code here is difficult to determine if it will work without seeing the entire db.
    Suggest you test and post with issues versus looking for answers before testing.

  5. #5
    Registered User
    Join Date
    08-31-2012
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    87

    Re: Access VBA to add a primary key (first field) after importing to access

    it works but it places the field at the end. so the primary key is at the last column at the moment. i want the primary key field to be at the first column instead at the end
    Last edited by Cutter; 09-15-2012 at 01:27 PM. Reason: Removed whole post quote

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,879

    Re: Access VBA to add a primary key (first field) after importing to access

    Here is a site you should visit to hone up on your SQL skills.

    http://www.sql-tutorial.com/

    I do not know of a way to put it where you want using SQL. Once it is added, you can do a make table query and move it to wherever you wish.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1