auto increment number/id

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 7
Reputation: ruudmu7 is an unknown quantity at this point 
Solved Threads: 0
ruudmu7 ruudmu7 is offline Offline
Newbie Poster

auto increment number/id

 
0
  #1
Jul 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: auto increment number/id

 
0
  #2
Jul 22nd, 2008
Hi,
Are you using Database? and Company_id is a Field?
KSG
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 7
Reputation: ruudmu7 is an unknown quantity at this point 
Solved Threads: 0
ruudmu7 ruudmu7 is offline Offline
Newbie Poster

Re: auto increment number/id

 
0
  #3
Jul 22nd, 2008
Originally Posted by selvaganapathy View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: auto increment number/id

 
0
  #4
Jul 22nd, 2008
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
KSG
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 7
Reputation: ruudmu7 is an unknown quantity at this point 
Solved Threads: 0
ruudmu7 ruudmu7 is offline Offline
Newbie Poster

Re: auto increment number/id

 
0
  #5
Jul 22nd, 2008
Originally Posted by selvaganapathy View Post
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: auto increment number/id

 
0
  #6
Jul 22nd, 2008
i m with u selvaganapathy, Select Max of Company_id , split it between character and number then increment number with 1.
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: auto increment number/id

 
0
  #7
Jul 22nd, 2008
Sure!
  1. Private Function GetNextID(sID As String) As String
  2. Dim i As Integer
  3. Dim sTmp As String
  4.  
  5. sTmp = Mid(sID, 2)
  6. i = Val(sTmp) + 1
  7. sTmp = "C" & Format(i, "0#")
  8. GetNextID = sTmp
  9. End Function

To use this
First Get Last Company ID and Pass it to this function
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. MsgBox GetNextID ("C01")
KSG
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 7
Reputation: ruudmu7 is an unknown quantity at this point 
Solved Threads: 0
ruudmu7 ruudmu7 is offline Offline
Newbie Poster

Re: auto increment number/id

 
0
  #8
Jul 23rd, 2008
ok it works..thanx a lot.........
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 7
Reputation: ruudmu7 is an unknown quantity at this point 
Solved Threads: 0
ruudmu7 ruudmu7 is offline Offline
Newbie Poster

Re: auto increment number/id

 
0
  #9
Jul 23rd, 2008
Originally Posted by selvaganapathy View Post
Sure!
  1. Private Function GetNextID(sID As String) As String
  2. Dim i As Integer
  3. Dim sTmp As String
  4.  
  5. sTmp = Mid(sID, 2)
  6. i = Val(sTmp) + 1
  7. sTmp = "C" & Format(i, "0#")
  8. GetNextID = sTmp
  9. End Function

To use this
First Get Last Company ID and Pass it to this function
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,641
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: auto increment number/id

 
0
  #10
Jul 23rd, 2008
>> how is the coding to get last Company ID from sql database?
Use sql statement :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Select Max(field) from tablename
.
selvaganapathy was given the best answer to split company_id.
Last edited by Jx_Man; Jul 23rd, 2008 at 12:50 pm.
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC