By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

How can i make auto increment in my textbox vb.net and database of access

P: 1
When i run the program i got the patient id = 1 then register again then patient id = 2 but when i register again it remain 2 . what is the problem with my code ?

Expand|Select|Wrap|Line Numbers
  1. Try
  2.             cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\HMSdb.accdb")
  3.             cn.Open()
  4.             cmd = New OleDbCommand("select * from PatientRegistration ", cn)
  5.             Dim dr As OleDbDataReader = cmd.ExecuteReader
  6.             If dr.Read Then
  7.                 txtPatientID.Text = dr.Item(0) + 1
  8.             Else
  9.                 txtPatientID.Text = "1"
  10.             End If
  11.         Catch ex As Exception
  12.             MsgBox(ex.Message)
  13.         End Try
  14.  
Sep 15 '15 #1
Share this Question
Share on Google+
1 Reply


100+
P: 299
Expand|Select|Wrap|Line Numbers
  1. txtPatientID.Text = dr.Item(0) + 1
I think this is your issue. dr.Item(0) is selecting the first record and adding 1 to it every time, which will always equal 2. You need to have it setup with an integer variable and do something like this:

Expand|Select|Wrap|Line Numbers
  1. Dim i as integer
  2. txtPatientID.Text = dr.Item(i)
  3. i = i + 1
  4.  
The integer variable should be global.
Sep 15 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.