Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make a custom ID Number using year and integer. This is my code:

VB
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"

textboxIDNumber.Text = idnumber

Connection()
sql = "SELECT * FROM Info WHERE idnumber LIKE '" & textboxIDNumber.Text & "'"
rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
If rs.Fields(0).Value <> idnumber Then
    rs.AddNew()
    rs.Fields(0).Value = textboxIDNumber.Text
    rs.Fields(1).Value = textboxLastName.Text
    rs.Fields(2).Value = textboxFirstName.Text
    rs.Fields(3).Value = textboxMiddleName.Text
    rs.Update()
    MsgBox("Successfully Added!", MsgBoxStyle.Information, "Message")
Else
    If rs.Fields(0).Value = idnumber Then
        rs.AddNew()
        textboxIDNumber.Text += 1
        rs.Fields(0).Value = textboxIDNumber.Text
        rs.Fields(1).Value = textboxLastName.Text
        rs.Fields(2).Value = textboxFirstName.Text
        rs.Fields(3).Value = textboxMiddleName.Text
        rs.Update()
        MsgBox("Successfully Added!", MsgBoxStyle.Information, "Message")
    End If
End If
    conn.Close()

I want to add an auto generated id number of a certain individual using YEAR and 4 digit integer to be exact.
VB
Dim idnumber As Integer
Dim yrVal As String = DateTime.Now.Year
idnumber = yrVal + "0001"

What I want is that if I register a person for the first time then his/her id number is 20180001 and the second person is 20180002, 0003, 004 and so on, but when the year is 2019 then it start again with 20190001, 20190002, 20190003 and so on. In my code as shown above it will increment from 20180001 to 20189999. I'm a beginner to this and it's hard to figure out.

What I have tried:

https://www.codeproject.com/Questions/1039035/How-do-I-generate-auto-number-in-vb-net

vb.net - How to generate Custom ID - Stack Overflow[^]

I tried the solutions link above because its a little related to my system but it is using OLeDBAdapter, dataset, datagrid and I don't know how to convert it to ADODB. I added ADODB reference. I'm a beginner and a little bit confused.
Posted
Updated 4-May-18 23:26pm
v2
Comments
Richard MacCutchan 5-May-18 4:55am    
You need to save the sequence number in the database in order to allocate each one. However, you need to be aware that this could end up generating the same value for multiple customers, unless you add some strong single-thread logic around it.

1 solution

One way to do this would be to create a table containing the next free number. In the simplest form the table column would already contain the year so the value could be like
20180019

Now each time a new row is added the logic would be following
- Start a transaction
- Update the free number row, add 1 to it
- Select the value from the table
- Insert the new row using the selected value
- Commit the transaction.

However I woudn't advice to use such column in the target table. What happens if you need more than 9999 rows in the table per year? You would need to add different kinds of checks and possibly change the database schema.

Instead I would recommend using a Surrogate key - Wikipedia[^]

As a side note, because you concatenate values from UI directly to the SQL statement your code is vulnerable to SQL injections. See SQL injection - Wikipedia[^] The preferred way is to use parameters, have a look at CreateParameter Method (ADO) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900