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.
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
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.
You have to pass it the current max id and it will return the next one.
Sir, how am I supposed to do that?
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).
Thank you sir, I'll try this out.
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?
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?
What I want is a continuous incrementation that does not limit at EMP011. Any idea how to do that?
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.