943,823 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Jul 22nd, 2008
0

auto increment number/id

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ruudmu7 is offline Offline
7 posts
since Jul 2008
Jul 22nd, 2008
0

Re: auto increment number/id

Hi,
Are you using Database? and Company_id is a Field?
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Jul 22nd, 2008
0

Re: auto increment number/id

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ruudmu7 is offline Offline
7 posts
since Jul 2008
Jul 22nd, 2008
0

Re: auto increment number/id

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
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Jul 22nd, 2008
0

Re: auto increment number/id

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ruudmu7 is offline Offline
7 posts
since Jul 2008
Jul 22nd, 2008
0

Re: auto increment number/id

i m with u selvaganapathy, Select Max of Company_id , split it between character and number then increment number with 1.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,140 posts
since Nov 2007
Jul 22nd, 2008
0

Re: auto increment number/id

Sure!
VB Syntax (Toggle Plain Text)
  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")
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Jul 23rd, 2008
0

Re: auto increment number/id

ok it works..thanx a lot.........
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ruudmu7 is offline Offline
7 posts
since Jul 2008
Jul 23rd, 2008
0

Re: auto increment number/id

Sure!
VB Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ruudmu7 is offline Offline
7 posts
since Jul 2008
Jul 23rd, 2008
0

Re: auto increment number/id

>> 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.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,140 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: datagrid to textbox and vice versa
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: ADODB Update with SQL





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC