We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,654 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to generate Auto Number(Id) in vb6

Hi, guys I am working on vb project, which will save client database.

I've done the coding of ADD, NEW, SAVE, EDIT button. Now, I am stuck at DELETE button. When I delete any particular client then I want my client_id (it's a column in my access database) should get updated (like if I have five clients in database and if I delete 3rd client, then the client_id should start from 1,2,3,4; not like 1,2,4,5)

I am using ADODB and Ms-access at backend, please help!!!!!

for DELETE, I've done following coding:

Private Sub cmdDelete_Click()

If rs.State = 1 Then rs.Close
admincon.Execute "delete from clientdetails where client_id=" & (lblId.Caption) & ""
MsgBox ("Record is Deleted successfully")
Clear
generateid ' It's a function i am using for generating ids. It's coding is given follow
Unload Me
frmAddClient.Show

End Sub

And for function generateid, I've done following coding:

Public Sub generateid()
Dim i As Integer
i = 1
If rs.State = 1 Then rs.Close
rs.Open "clientdetails", admincon, adOpenDynamic, adLockOptimistic, adCmdTable
Do
admincon.Execute "update clientdetails set client_id=" & i & ""

i = i + 1
rs.movenext

Loop While rs.EOF = True

End Sub
4
Contributors
8
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
17
Views
Question
Answered
Amiet Mhaske
Newbie Poster
20 posts since Nov 2011
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

Hi, guys I am working on vb project, which will save client database.

I've done the coding of ADD, NEW, SAVE, EDIT button. Now, I am stuck at DELETE button. When I delete any particular client then I want my client_id (it's a column in my access database) should get updated (like if I have five clients in database and if I delete 3rd client, then the client_id should start from 1,2,3,4; not like 1,2,4,5)

No it shouldn't. Once assigned, a client ID should never be changed. It is their unique value used throughout the system. All records pertaining to that person should be using this number and not their name, so the change would be monumental in a 'real' database.

What do you think would happen in the real world if, when someone stops driving, all the driver's license numbers changed?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

WaltP is very right. Please all IDs should remain unique

Netcode
Veteran Poster
1,037 posts since Jun 2009
Reputation Points: 43
Solved Threads: 70
Skill Endorsements: 0

Hi,

Yes, Client_ID's should be Unique.
But still if, you want to renumber them, Change your code to :

Public Sub generateid()
    Dim i As Integer
    i = 1
    If rs.State = 1 Then rs.Close
    rs.Open "clientdetails", admincon, adOpenDynamic, adLockOptimistic
    Do While Not rs.EOF
       rs("Client_ID") = i
       rs.Update
       i = i + 1
       rs.MoveNext
    Loop 
End Sub

Regards
Veena

QVeen72
Veteran Poster
1,017 posts since Nov 2006
Reputation Points: 118
Solved Threads: 164
Skill Endorsements: 5

Hi,

Yes, Client_ID's should be Unique.
But still if, you want to renumber them, Change your code to :

Public Sub generateid()
    Dim i As Integer
    i = 1
    If rs.State = 1 Then rs.Close
    rs.Open "clientdetails", admincon, adOpenDynamic, adLockOptimistic
    Do While Not rs.EOF
       rs("Client_ID") = i
       rs.Update
       i = i + 1
       rs.MoveNext
    Loop 
End Sub

Regards
Veena

But what about all the other database records in the other tables that have the ID in them?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

WaltP, actually in my case client ID is not a primary key. It's actually requirement of my project that when i delete the customer client id should get updated.

Amiet Mhaske
Newbie Poster
20 posts since Nov 2011
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

let me first try this code

Amiet Mhaske
Newbie Poster
20 posts since Nov 2011
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

Hi,

Yes, Client_ID's should be Unique.
But still if, you want to renumber them, Change your code to :

Public Sub generateid()
    Dim i As Integer
    i = 1
    If rs.State = 1 Then rs.Close
    rs.Open "clientdetails", admincon, adOpenDynamic, adLockOptimistic
    Do While Not rs.EOF
       rs("Client_ID") = i
       rs.Update
       i = i + 1
       rs.MoveNext
    Loop 
End Sub

Regards
Veena

You are awesome man, that code worked well for me. Thanks a lot!!!

Amiet Mhaske
Newbie Poster
20 posts since Nov 2011
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0

But what about all the other database records in the other tables that have the ID in them?

Thanks for your help

Amiet Mhaske
Newbie Poster
20 posts since Nov 2011
Reputation Points: 6
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by WaltP, QVeen72 and Netcode

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0800 seconds using 2.7MB