Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
INTRODUCTION AND RELEVANT INFORMATION:

I have MS ACCESS 2007 database that I edit using ADO and C++.

PROBLEM:

My problem is that primary key also represents an ordinal number of the record, and after deletion, it should be properly updated. Primary key is of autonumber type.

Here is an example of what I am talking about:
C++
| #PK | Other data ... |
|  1  |      ...       |
|  2  |      ...       |
|  3  |      ...       |
|  4  |      ...       |
|  5  |      ...       |

Now if I delete the 3rd record I get the following problem:
C++
| #PK | Other data ... |
|  1  |      ...       |
|  2  |      ...       |
|  4  |      ...       |
|  5  |      ...       |

but I should get the following result:
C++
| #PK | Other data ... |
|  1  |      ...       |
|  2  |      ...       |
|  3  |      ...       | // edited to reflect the change ( previous value was 4 )
|  4  |      ...       | // edited to reflect the change ( previous value was 5 )

If I delete last record and then insert new one I get this result:

C++
| #PK | Other data ... |
|  1  |      ...       |
|  2  |      ...       |
|  3  |      ...       |  
|  4  |      ...       | 
|  6  |      ...       |  // this should be 5

QUESTIONS:

Is there a way for me to programmatically update the autonumber field after I perform the DELETE query ?

I am well aware this is bad practice, so I would prefer adding new field that should be ordinal number so my table can look like this:
C++
| #PK | Ordinal | Other data ... |
|  1  |    1    |      ...       |
|  2  |    2    |      ...       |
|  4  |    3    |      ...       | 
|  5  |    4    |      ...       | 

but I would prefer it to update itself automatically. If this is not possible, I would prefer to update the field with SQL query after I perform the deletion.

Thank you.

Best regards.
Posted
Updated 11-Apr-14 13:38pm
v2
Comments
[no name] 11-Apr-14 19:43pm    
http://www.google.com/search?q=access+renumber+autonumber
AlwaysLearningNewStuff 12-Apr-14 6:34am    
Please reread my answer, only this time do it carefully.

I did try Google, but got nothing that could help me to update primary key programmatically. Therefore came my second question, which I believe is a better approach as I have explained.

Best regards.
PIEBALDconsult 11-Apr-14 20:05pm    
That's a very bad idea.
AlwaysLearningNewStuff 12-Apr-14 6:36am    
I used Google to find a solution, but got nothing that could help me to update primary key programmatically. Therefore came my second question, which I believe is a better approach than the first one.

Best regards.

If you only need the ordinal field for reports/display would a query with a calculated field be a possible solution?

I haven't got a copy of Access to hand, but the T-SQL for this approach would be as shown below. Where I've created a view you'd create your Access query and use that instead of the table as the basis for reports/display. With luck the conversion of the T-SQL view to Access query shouldn't be impossible.

SQL
create table a
(
  -- T-SQL equivalent of autonumber
  pk int identity (1,1),
  data varchar(20)
)

create view ordinalA as
select
  -- Our calculated index field
  count(prev.pk) + 1 as [index],
  main.pk, 
  main.data
from a as main
left join a as prev on prev.pk < main.pk
group by main.pk, main.data 
go

Some data to play around with.
SQL
insert into a (data) values ('alpha')
insert into a (data) values ('beta')
insert into a (data) values ('gamma')
insert into a (data) values ('delta')
insert into a (data) values ('epsilon')
insert into a (data) values ('zeta')

Are the table and view/query in agreement?
SQL
select * from a
select * from ordinala

pk          data
----------- --------------------
1           alpha
2           beta
3           gamma
4           delta
5           epsilon
6           zeta

index       pk          data
----------- ----------- --------------------
1           1           alpha
2           2           beta
4           4           delta
5           5           epsilon
3           3           gamma
6           6           zeta

Now throw away a row.
SQL
delete a where pk = 3

select * from a

pk          data
----------- --------------------
1           alpha
2           beta
4           delta
5           epsilon
6           zeta

And with luck the view/query should be correctly indexed.
SQL
select * from ordinala

index       pk          data
----------- ----------- --------------------
1           1           alpha
2           2           beta
3           4           delta
4           5           epsilon
5           6           zeta
 
Share this answer
 
Comments
PIEBALDconsult 12-Apr-14 11:15am    
With SQL Server you could use the ROW_NUMBER() function.
cigwork 12-Apr-14 15:03pm    
Indeed you could, but the problem is in Access so the query needs to be something that stands a chance of being converted to Access
AlwaysLearningNewStuff 17-Apr-14 12:38pm    
Thank you for your help. 5ed. Best regards.
I have decided to add a new field in my table that will hold the ordinal number of the record.

If we assume the field is named OrdinalNumber then the following solution worked for me:

C++
// when inserting record, I just had to add COUNT( PK ) + 1
INSERT INTO MyTable (  OrdinalNumber , ... ) SELECT COUNT( PK ) + 1 , ...  
    from  MyTable ;

// when deleting, I had to perform following two queries :
DELETE from MyTable where PK = ? ;

// decrement all the successors ordinal number by one
UPDATE MyTable set OrdinalNumber = (  OrdinalNumber - 1 ) where ( PK > ?  );


Everything seem to work well. I wish there was an easier way though...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900