Results 1 to 3 of 3

Thread: Create my own AutoNumber Field with AbsolutePosition

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Question Create my own AutoNumber Field with AbsolutePosition

    I wanna create my own AutoNumber Field with AbsolutePosition function. So it always is in numeric order and no number been skipped.
    I do like this, but it doesen't work:
    VB Code:
    1. Dim Db As Database
    2. Dim rs As Recordset
    3. Dim SQL, SQL1 As String
    4. Set Db = CurrentDb
    5. SQL = "SELECT * From Table1"
    6. Set rs = Db.OpenRecordset(SQL)
    7.  
    8. With rs
    9.  
    10.         .MoveLast
    11.         .MoveFirst
    12.  
    13. Do While Not .EOF
    14.  
    15.              SQL1 = "UPDATE Table1 SET Table1.[IDT]=" & "'" & .AbsolutePosition + 1 & "'"
    16.              Db.Execute SQL1
    17.            
    18. .MoveNext
    19.         Loop
    20.  
    21.         .Close
    22.    
    23. End With
    24.  
    25.  
    26. Set Db = Nothing
    27. Set rs = Nothing
    I guess I missed that I should have any criteria somewhere, but I dont know how I should do.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    You need to add the Where clause. Always try to use the table's primary key (I am assuming its IDT).

    VB Code:
    1. SQL1 = "UPDATE Table1 SET Table1.[IDT]=" & "'" & .AbsolutePosition + 1 & "'"
    2.  
    3. SQL1 = SQL1  & " Where Table1.[IDT] = " & "'" & .Fields("IDT").Value & "'"

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Thanks brucevde!

    I have found another solution also:
    VB Code:
    1. Dim Db As Database
    2. Dim rs As Recordset
    3. Dim SQL As String
    4. Set Db = CurrentDb
    5. SQL = "SELECT * From Table1"
    6. Set rs = Db.OpenRecordset(SQL)
    7.  
    8. With rs
    9.        .MoveLast
    10.        .MoveFirst
    11. Do While Not .EOF
    12.         .Edit
    13.         !IDT = .AbsolutePosition + 1
    14.         .Update
    15.   .MoveNext
    16.         Loop
    17.   .Close
    18. End With
    19.  
    20. Set Db = Nothing
    21. Set rs = Nothing
    Last edited by Pirre001; Jan 11th, 2003 at 03:27 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
  •  



Click Here to Expand Forum to Full Width