BP Forums
auto generated number - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9)
+----- Thread: auto generated number (/showthread.php?tid=664)



auto generated number - jochen29 - 09-29-2012

i just wanna ask how to code an auto-numbering?

lets just say it start at 000000 and then if you add another the counter add by 1.. how can i do that? and how
to know where the counter stops? so that it doesnt back to 000000.


Re: auto generated number - brandonio21 - 09-30-2012

Are you trying to add 1 to a variable? To a textbox text field? To a label?


Re: auto generated number - jochen29 - 10-01-2012

im trying to add 1 in the variable.

let say.. the last added value is 10003 and then if
i add another entry the data or code should know that the last added value is 10003 so that when i added another entry the variable counter should be 10004..


Re: auto generated number - brandonio21 - 10-01-2012

Well, first of all, you would need an integer variable to keep track of the number, so we can do this with something like this:
[code2=vbnet]Private Counter As Integer = 0[/code2]

Then, Every time we need to increment the variable, we can do so with:
[code2=vbnet]Counter += 1[/code2]

However, when we display the variable to the user, we would like it to be in the proper format. Since your number is 6 characters long, we pad left by 6 characters and fill any empty spots with 0s. Like so,
[code2=vbnet]Dim cString As String
cString = Counter.ToString.PadLeft(6, "0"c)[/code2]

Hopefully this helps.