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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prince29
    New Member
    • Sep 2015
    • 1

    #1

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

    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 ?

    Code:
    Try
                cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\HMSdb.accdb")
                cn.Open()
                cmd = New OleDbCommand("select * from PatientRegistration ", cn)
                Dim dr As OleDbDataReader = cmd.ExecuteReader
                If dr.Read Then
                    txtPatientID.Text = dr.Item(0) + 1
                Else
                    txtPatientID.Text = "1"
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    Last edited by Rabbit; Sep 15 '15, 03:47 PM. Reason: fixed code tags
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    Code:
    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:

    Code:
    Dim i as integer
    txtPatientID.Text = dr.Item(i)
    i = i + 1
    The integer variable should be global.
    Last edited by Luk3r; Sep 15 '15, 03:07 PM. Reason: Typo of "record".

    Comment

    Working...