please help me to do with my system.
i want my txtempid.Text is auto generated evrytime i will add new employee .
Field = "emp_id"
i'm using vb 6 as ADODB and Mysql database

thanks in advance...God Bless

Recommended Answers

All 6 Replies

very simple my friend.

First add a dummy ID in the databse

for eg,

empid empname

1 admin

then before insert find the max(empid)

then add one to it.

and include it in every query you process.

This can be done in frontend.

I am not the one who can help in databse side.

But I am sure that autoincrement is there at mysql I hope.

May be the one who is good at database can help you on that....

Hope this helps you...

Have a happie coding...:=D

commented: old methodology, but it will work i hope. +0
Dim rs As Recordset, nextId As Long
Set rs = con.Execute("SELECT Max(emp_id) As LastID FROM theTable")
nextId = 1
If rs.RecordCount <> 0 Then
    nextId = rs!LastID
End If
nextId = nextId + 1

@RonalBertogi

why nextId = 1 ?

remove 3rd Line.
Because when the table is with no data then nextid will be 2(due to nextId initialise with 1 and at last incremented by 1) which is not right. it should be 1 when table is empty.

commented: Your right, nextId must be initially 0 +3

If rs.State = adStateOpen Then rs.Close
rs.Open "select max(emp_id) from tablename", db, adOpenDynamic, adLockReadOnly
If rs(0) <> 0 Then txtempid.Text = rs(0) + 1 Else txtempid.Text = 1

u can also use identity field to auto generate ur field from sql .no need to write code.in sql select field and from properties put identity specification as yes..then it will autogenerate ur field as per ur record

u can also use identity field to auto generate ur field from sql .no need to write code.in sql select field and from properties put identity specification as yes..then it will autogenerate ur field as per ur record

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.