Athirah Hazira
Athirah Hazira

Reputation: 473

Find maximum number in column and add one (Access & VB.NET)

The following is my code:

Page Load

Private Sub frm_addproduct_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        txt_id.Text = generate_id()
    End Sub

Generate ID

Private Function generate_id() As String
        Dim lastid As String = run_sql_query("SELECT MAX(FLD_PRODUCT_ID) AS LASTID FROM TBL_PRODUCTS").Rows(0).Item("LASTID")
        'MsgBox(lastid)
        Dim newid As String = "" & Mid(lastid, 2) + 1
        Return newid
    End Function

My problem is that when it displays the id inside the txt_id textbox, and for example the last id is 45 it only shows "6" when it supposed to be "46". What is missing from here?

Upvotes: 0

Views: 674

Answers (1)

Cyval
Cyval

Reputation: 2519

Dim lastId as string = "SELECT MAX(FLD_PRODUCT_ID)+1 FROM tablename. . . ."; Return lastId

Or if the database requires an int, you may convert it as well.

Upvotes: 1

Related Questions