0

Hi guys I need help from you. How do i increment the id like for example: EMP001, EMP002, EMP003 I hope you can help me out guys, thanks in advanced.

2
Contributors
10
Replies
29
Views
1 Year
Discussion Span
Last Post by Reverend Jim
Featured Replies
  • Try Private Function NextID(empid As String) As String Dim n As Integer = CInt(empid.Substring(3)) Return empid.Substring(0, 3) & Format(n + 1, "000") End Function Test with MsgBox(NextID("EMP013")) Read More

  • First of all, you should have specified that this is (probably) a database question, plus, what type of database. You can't autoincrement a string field. You'd have to specify empid as a numeric (int). if you want to use `EMP###` as the format then you can calculate that on select … Read More

1

Try

Private Function NextID(empid As String) As String

    Dim n As Integer = CInt(empid.Substring(3))
    Return empid.Substring(0, 3) & Format(n + 1, "000")

End Function

Test with

MsgBox(NextID("EMP013"))

Edited by Reverend Jim

0

Hello sir thanks for the reply. I tried the code and it works however it is not incrementing everytime I saved data. like for example the id was saved as EMP001 and the next would be EMP002, the ID was stuck at EMP001 and it doesn't increment.

Edited by Gilbert_3

1

First of all, you should have specified that this is (probably) a database question, plus, what type of database. You can't autoincrement a string field. You'd have to specify empid as a numeric (int). if you want to use EMP### as the format then you can calculate that on select as

SELECT empid = 'EMP' + FORMAT(empid,'000'), empname 
  FROM Table_1

This is the syntax for MS SQL (the only database engine I have installed).

Edited by Reverend Jim

0

Sir I tried it out with these codes:

Private Sub frmEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  txtEmpID.Text = (NextID("EMP000"))

   End Sub


  Private Function NextID(ByVal empid As String) As String
        Dim n As Integer = CInt(empid.Substring(3))
        Return empid.Substring(0, 3) & Format(n + 1, "000")
        n = "SELECT empid = 'EMP' + FORMAT(empid,'000') FROM tbl_employee"
    End Function
    end class

It's still doesn't increment and stuck at EMP001.
Where did I got wrong in this codes sir?

Edited by Gilbert_3

0

If you keep passing NextID the same string (EMP000) then, of course you are going to keep getting back EMP001. If you run the following code:

Dim empid As String = "EMP001"

For i As Integer = 1 To 10
    empid = NextID(empid)
    MsgBox("next ID = " & empid)
Next

you will generate EMP002 through EMP011. Isn't that what you want?

Edited by Reverend Jim

0

What I want is a continuous incrementation that does not limit at EMP011. Any idea how to do that?

Edited by Gilbert_3

0

I hate to sound rude, but anyone who knows anything about programming should be able to take my example and apply it. You take the largest empID in use and pass it to NextID and it gives you the next available empID. If you don't know how to get the largest empID, the SQL query is

SELECT MAX(empID) FROM myTable

If you still don't get it then you should be doing a lot more reading books about Beginning vb.Net Programming before tackling a database project. I provided you with a function that takes an empID string and generates the next one in the sequence. I showed you how to call it with a value and what value it returns as a result. I really don't see what else I can do or how much clearer I can be.

Edited by Reverend Jim

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.