Results 1 to 9 of 9
  1. #1
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28

    Increment the Primary Key from Excel

    This is VBA from Excel. I want to know how to increment the [ID] for each value of the range.

    I have tried "NULL".

    I have tried adding "n=n+1" after the "for each" and "thisSQL = "INSERT INTO... VALUES (" & n & ",..."



    Code:
    For Each value In Range("D5:D" & Cells(Rows.Count, "D").End(xlUp).Row)
            accDate = Cells(value.Row, 1)
            accDesc = Cells(value.Row, 2)
            accData = Cells(value.Row, 3)
            thisSQL = "INSERT INTO [Table1] ([ID], [Date], [Desc], [Data]) VALUES ("What goes here? ", #" & Format(CDate(accDate), "MM/DD/YY") & "#, '" & accDesc & "', " & accData & ");"
            conn.Execute CommandText:=thisSQL
    Next
    Last edited by Philosophaie; 08-22-2012 at 02:15 PM.

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Where is the initial starting ID value coming from?
    If this ID number is just an AutoNumber in your Access table, you don't need to account for it in your INSERT statement, Access will take of it automatically. Just take it out of your INSERT INTO and VALUES clauses altogether.

  3. #3
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28
    I took out the [ID] in fields and in VALUES - Error code:

    Number of query values and destination fields are not the same.
    If some please could tell me what I can put in "What goes here? " (above).

    The
    [ID] field must have some property that I may be overlooking.

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    We can't tell you that until you tell us where the ID value is coming from (see my previous post)!

    Is it an Autonumber, or is the initial/starting value being pulled from somewhere?
    If it is being pulled from somewhere, then where?
    Or do you just want it to start at 1?

  5. #5
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28
    [ID] is the primary key it is first of all the fields which I pre-programmed in Table1. I can not get it to autonumber:

    thisSQL = "INSERT INTO [Table1] ([Date], [Desc], [Data]) VALUES (#" & Format(CDate(accDate), "MM/DD/YY") & "#, '" & accDesc & "', " & accData & ");"
    This give this error: "Number of query values and destination fields are not the same."

    n=n+1
    thisSQL = "INSERT INTO [Table1] ([ID], [Date], [Desc], [Data]) VALUES (" & n & ", #" & Format(CDate(accDate), "MM/DD/YY") & "#, '" & accDesc & "', " & accData & ");"
    This give this error: "Syntax error in INSERT INTO statement."

    I deduce that " & n & " is the source of the error.

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If it is an Autonumber, leave it out completely. Access will default it and handle it on its own, no need to include it in the INSERT statement, i.e.

    Code:
    thisSQL = "INSERT INTO [Table1] ([Date], [Desc], [Data]) VALUES ("#" & Format(CDate(accDate), "MM/DD/YY") & "#, '" & accDesc & "', " & accData & ");"

  7. #7
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28
    Error in your autonumber:


    thisSQL = "INSERT INTO [Table1] ([Date], [Desc], [Data]) VALUES (#" & Format(CDate(accDate), "MM/DD/YY") & "#, '" & accDesc & "', " & accData & ");"


    This give this error: "Number of query values and destination fields are not the same."

    I had already pre-programmed the fields into Access. Tried without pre-programming, got error.

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    After you set the thisSQL variable, if you run:
    Code:
    MsgBox thisSQL
    what exactly does it return?
    If you post it here, we can analyze your code to see if you are building valid syntax.

  9. #9
    Philosophaie is offline Novice
    Windows Vista Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    28
    Thank you. I got it to work.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 05-07-2012, 12:08 PM
  2. Increment Field Value
    By Malseun in forum Access
    Replies: 9
    Last Post: 02-03-2012, 04:55 AM
  3. Date Increment
    By James Tebb in forum Access
    Replies: 1
    Last Post: 04-11-2011, 11:40 AM
  4. Need Frequent Excel Imports with Primary Keys
    By UMassEngineer in forum Import/Export Data
    Replies: 1
    Last Post: 03-27-2011, 10:07 AM
  5. Replies: 5
    Last Post: 11-12-2010, 12:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums