954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

auto increment number/id

Hi,
I'm currently using vb6. i need help.
How can I increment a number in a textbox/label everytime when the form is loaded?
Suppose company_id
I want that first time when the form gets loaded Customer_id should appear in the textbox/label as C01.
Second time it should appear as C02.
Third time C03 and vice versa.
Please help.

ruudmu7
Newbie Poster
7 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Hi,
Are you using Database? and Company_id is a Field?

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 
Hi, Are you using Database? and Company_id is a Field?


yess...sql.
it is possible we can do auto increment in the database right?but it will just add 1 by 1 and only using 1,2,3 n so on..i want it to be in the other format like C01,C02 and so on..so we must do the coding in the form right?

ruudmu7
Newbie Poster
7 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Yes, Auto increment is possible, but it should be a number, i Think so. But in your case You have mixed the Number and String. So you have to do

> Get the Last Company_id
> Split it into String and Number ( C01 as C and 01)
> Then Increment the Value of Number
> Finally Concatenate the Values

If "C" prefix is fixed for all ID then the extraction is easy.

To extract Use Left () or Mid() function and to Format numbers use Format() function in final concatenation

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

Yes, Auto increment is possible, but it should be a number, i Think so. But in your case You have mixed the Number and String. So you have to do > Get the Last Company_id > Split it into String and Number ( C01 as C and 01) > Then Increment the Value of Number > Finally Concatenate the Values

If "C" prefix is fixed for all ID then the extraction is easy.

To extract Use Left () or Mid() function and to Format numbers use Format() function in final concatenation


ok thanx...err can u give an example with the code?tq so much

ruudmu7
Newbie Poster
7 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

i m with u selvaganapathy, Select Max of Company_id , split it between character and number then increment number with 1.

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Sure!

Private Function GetNextID(sID As String) As String
   Dim i       As Integer
   Dim sTmp    As String
   
   sTmp = Mid(sID, 2)
   i = Val(sTmp) + 1
   sTmp = "C" & Format(i, "0#")
   GetNextID = sTmp
End Function


To use this
First Get Last Company ID and Pass it to this function

MsgBox GetNextID ("C01")
selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

ok it works..thanx a lot.........

ruudmu7
Newbie Poster
7 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Sure!

Private Function GetNextID(sID As String) As String
   Dim i       As Integer
   Dim sTmp    As String
   
   sTmp = Mid(sID, 2)
   i = Val(sTmp) + 1
   sTmp = "C" & Format(i, "0#")
   GetNextID = sTmp
End Function

To use this First Get Last Company ID and Pass it to this function

MsgBox GetNextID ("C01")


allo...err how is the coding to get last Company ID from sql database?is Company ID must be set to primary key?

ruudmu7
Newbie Poster
7 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

>> how is the coding to get last Company ID from sql database?
Use sql statement :

Select Max(field) from tablename


.
selvaganapathy was given the best answer to split company_id.

Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

Hi,

I'm currently using VB 6.0 in front hand and ORACLE 8i IN back hand, i need help.

How can I increment a MOVEMENT TICKET NO. (MT_NO) i.e. ITS TYPE IS VARCHAR i.e. (L/08-09/MT_NO) & (M/08-09/MT_NO) BOTH at a time in TWO DIFFERENT TEXT BOXES everytime when the form is loaded?

Suppose I want that first time when the form gets loaded MOVEMENT TICKET NO. (VARCHAR) i.e. (L/08-09/MT_NO) & (M/08-09/MT_NO) should appear in the TWO DIFFERENT textbox .

ONE FOR L/08-09/MT_NO
&
OTHER FOR M/08-09/MT_NO


Second time it should INCREMENT the MT_NO in both the text boxes and so on.

PLEASE HELP.
early reply is awaited

DEEPAK ANAND

deepak.anand71
Newbie Poster
1 post since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

plz tell me the code of vb6 when i add new record to the database it auto increment the number in id textbox

khan.777
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You