You will need to create table with an integer field with a default of 0.
On form_load you will want to pull the value from the database and add 1 to it.
On form_close you will want to save that value back to the database.
You can use my previous post here for the connection example.
You will do something like this:
'In the form load event.
sqlcmd = "SELECT curnum FROM table"
cmd = new OleDBCommand(sqlcmd,con)
'In the form close event.
sqlcmd = "UPDATE table SET curnum ='" & txtStudentID.text & "'"
cmd = new OleDBCommand(sqlcmd,con)
Begginnerdev
Practically a Posting Shark
892 posts since Apr 2010
Reputation Points: 198
Solved Threads: 149
Skill Endorsements: 9
We rarelly want to increment on form open or in any event other than the actual insert. The reason for this is that you are probably going to get either multiple records with the same id (if not set as unique in a multiuser environment) or one of your users will get an error (again in a multiuser environment). Think of it as a user opens your form and it takes him 5 mins to do the insert, if during those 5 mins another user opens your form he'll get the same id as the 1st one.
If you choose to store the "current" id in another table and increment it the same time you are retrieving it then you'll probably have chunks of numbers missing if the user decides not to go ahead with the insert.
My advice would be to let SQL assign it's ID and you retrieve it with OUTPUT in your insert statement. This way your key will be unique and with a continuation, allowing you to know if records are missing among other things.
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
You can try loading the form, without getting the id.
When the user clicks "Save" you will retrieve the id then save.
That way you can prevent what adam_k is talking about.
The only way to receive the same key would be if they both press the buttons at the same time.
Begginnerdev
Practically a Posting Shark
892 posts since Apr 2010
Reputation Points: 198
Solved Threads: 149
Skill Endorsements: 9
Is this a multiuser environment? How many users/inserts per day or hour top?
What will you get out of displaying the next ID to be assigned? What do you plan to do with it?
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
I understand, but I don't see a reason for the ID to be on the form when opened.
For example:
CREATE TABLE students
(id int IDENTITY(1,1),
name varchar(200),
address varchar(200),
phone varchar(200))
insert into students (name, address,phone) values ('Test','Homeless','0')
By setting the ID as IDENTITY the SQL will assign the next value according to the seed and increment specified automatically. You don't have to pass the ID value from your form to the SQL.
If you don't want to do it with IDENTITY then:
CREATE TABLE students
(id int,
name varchar(200),
address varchar(200),
phone varchar(200))
insert into students (id,name, address,phone)
select max(id) +1, 'Test','Homeless','0' from students
This insert will instruct the SQL server to calculate the ID, without passing the actual number.
If you do want your ID to display on your form (although I strongly recommend against it) use:
select max(id) +1 from students
adjusted to your table, field names to get the new ID.
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
Just saw an error in my second insert, the one without identity. It should be:
CREATE TABLE students
(id int,
name varchar(200),
address varchar(200),
phone varchar(200))
insert into students (id,name, address,phone)
select max(id) +1, 'Test','Homeless','0'
(Without the from students)
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
Question Answered as of 1 Year Ago by
adam_k,
Begginnerdev
and
poojavb