Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I designed a booking program in VB.net and MS Access as database. I need a booking reference number to be automatic increase each time there's booking and must start from 100. With the code I tried it does increase when I run the program.The problem it always start from 100 anytime I run the program. See code I used

What I have tried:

Option Strict On
Option Explicit On

Public Class Form1
    Dim i As Integer = 100
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        REFNOTextBox.Text = CStr(i)
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        i = i + 1
         REFNOTextBox.Text = CStr(i)
    End Sub
End Class
Posted
Updated 8-May-20 2:49am

MS Access has an AutoNumber data type which would take care of this for you. You can set the initial value as well as its increment amount.
If down the road you need to scale up into a larger db like Sql Server; there is an Identity specification you can place on a numeric field with the same basic setup

References:
Add an AutoNumber field as a primary key - Access[^]
IDENTITY (Property) (Transact-SQL) - SQL Server | Microsoft Docs[^]
 
Share this answer
 
You have to write the counter in some place, then read it back when you run the program.
Use the database maybe?
 
Share this answer
 
Comments
NyikoB 8-May-20 7:36am    
I'm little bit lost. What actually mean when you say I have to write the counter? or use database?
Richard MacCutchan 8-May-20 8:59am    
That is not the correct way. See MadMyche's solution.
MadMyche 8-May-20 11:01am    
Thanks for the pitch!
Assume that you use a table in the database for keeping track of the index.
Follow these steps :
- On opening, check if there is an index record on the database
- If a record is found, set the "i" variable to the record value and add 1
- If there is no record, set the "i" variable to 1
- On each button click increase the "i" variable and store it's value to the record in the database.

(I assume that you know ho to read and write from access database)
 
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