Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vb to generate autonumber

Status
Not open for further replies.

pabby

IS-IT--Management
Jan 5, 2004
47
GB
Hi,

Beginner Alert !

How do I get a text box in VB6 to automatically generate an autonumber ?

Also how do I get a textbox to automatically show the time when the form loads?

thanks,

Paul
 
for the second point,
try : textbox.text = Format(Now, "hh:m:ss")
(choose the format you like)
in the load event of the form.
(Or did you mean you need something like a clock on the form ?)

Regarding the first point - is your application lined to a Database ?
 
For the autonumber you first need some place to store the last value of the autonumber. This can be a database, a file or in the registry. Then you just call the last number used, increment it and write the new value back.

zemp
 
It depends the procedure that you want to use if you want to be database driven, wich is better... go on Access or your DBMS and set the field to autonumber, and then do not include the field on the INSERT statement likes this

TABLE TEST (id autonumber, code string);

and then you insert code should be:

INSERT INTO TEST (CODE) VALUES('THECODE');

Or you can go in for a calculation like:

SELECT SUM(MAX(ID)+1) AS MAXIMUN FROM TEST;

But if you're planning to have a big database this operation can become slow.

===================================
Never underestimate the power of stupid people in big numbers
===================================
 
I have an acccess dbase and a vb6 front end - all very very simple stuff as I am a beginner.

I need to be walked through the process of how to connect the vb front end to the access dbase. Are there any documents that actually walk you through the process or is there anyone out there who could assist.

thanks again,

 
OK, the easy way, for very simple applications is to add an adodc control to your form (you probably have to add some component like Microsoft ADO Data Control ).
I never used this - but can be helpfull to do simple stuff - I am sure there a lots of example out there.

Or - some more code - but i believe more flexible :


connect to an access (2000) database :

Dim cnn1 As ADODB.Connection
dim strConnect as string
dim DatabaseLocation as string

Set cnn1 = New ADODB.Connection
If cnn1.State = 0 Then
Databaselocation="c:\test.mdb"
strConnect= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseLocation & ";Persist Security Info=False;Jet OLEDB:Database"
cnn1.CursorLocation = adUseClient
cnn1.Open strConnectionString
end if


Query the database :
Dim RS As ADODB.Recordset
Dim cmd As ADODB.Command
dim strSQl as string
strSQL="Select * from table1"
Set RS = New ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = cnn1
cmd.CommandText = strSQL
Set RS = New ADODB.Recordset
RS.CursorType = adOpenStatic
RS.LockType = adLockReadOnly
RS.Open cmd

' do things with RS

Set cmd = Nothing
Set RS = Nothing

'disconnect

If cnn1 <> Empty Then
If cnn1.State <> 0 Then
cnn1.Close
End If
Set cnn1 = Nothing
End If


(I use all this in class : connect at the start of the app - disconnect at the end)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top