How to get data from newly added row in mysql database using VB6.0

------_=_NextPart_001_01C5BAAF.0F14ED40
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I have this nagging problem with my VB6.0 application. I am creating a =
new record and I want to carry the record ID for future references down =
the code. This record ID is AUTO_INCREMENT and I am using MYSQL =
database. My code is as follows after the definition of my recordset and =
initialising it:
rs.addnew
rs!lastname =3D text1.text
rs!firstname=3Dtext2.text
rs.update
varRecID =3D rs!ClientID
rs.close

Now when I trace the program "varRecID" is zero(0), but if I browse the =
table in MYSQL I can see the record ID.

Where am I going wrong.

Thanks.


------_=_NextPart_001_01C5BAAF.0F14ED40--
Shepherd Madziwa [ Fr, 16 September 2005 13:09 ] [ ID #969562 ]

RE: How to get data from newly added row in mysql database using VB6.0

Try this:

varRecID =3D rs!ClientID
rs.update

MySQL assigns an autonumber id as soon as the new record created, even
though it may not be displayed and it's not officially the correct id
until the record is saved. Once you update, the record is no longer
active, so you can't see it without requerying. I ran into the same
problem with an Access application. It seems counter-intuitive to get
the id before the record is saved, but it does work.

Ethan Moe


-----Original Message-----
From: Shepherd Madziwa [mailto:SMadziwa [at] psmi.co.zw]
Sent: Friday, September 16, 2005 4:09 AM
To: MYODBC [at] LISTS.MYSQL.COM
Subject: How to get data from newly added row in mysql database using
VB6.0
Importance: High

I have this nagging problem with my VB6.0 application. I am creating a
new record and I want to carry the record ID for future references down
the code. This record ID is AUTO_INCREMENT and I am using MYSQL
database. My code is as follows after the definition of my recordset and
initialising it:
rs.addnew
rs!lastname =3D text1.text
rs!firstname=3Dtext2.text
rs.update
varRecID =3D rs!ClientID
rs.close

Now when I trace the program "varRecID" is zero(0), but if I browse the
table in MYSQL I can see the record ID.

Where am I going wrong.

Thanks.




--
MySQL ODBC Mailing List
For list archives: http://lists.mysql.com/myodbc
To unsubscribe: http://lists.mysql.com/myodbc?unsub=3Dgcdmo-myodbc [at] m.gmane.o rg
Ethan Moe [ Fr, 16 September 2005 17:57 ] [ ID #969563 ]

RE: How to get data from newly added row in mysql database using VB6.0

The approach I use would go like this.

varRecId =3D GetLastInsertID

where GetLastInsertID looks like this

Public Function () As Long
Dim rs As MYSQL_RS

Set rs =3D RunSQL("SELECT LAST_INSERT_ID() AS ID")
GetLastInsertID =3D CLng(rs.Fields(0).Value)
rs.CloseRecordset
End Function

Depending on how you are connecting to MySQL you may need to adjust this
routine. I am using VBMySQLDirect. If you are using ADO then the routine
would go like this.

Public Function () As Long
Dim rs As ADODB.Recordset

Set rs =3D gConn.Execute("SELECT LAST_INSERT_ID() AS ID")
GetLastInsertID =3D CLng(rs.Fields(0).Value)
rs.Close
End Function

gConn is the current connection object you are using.

John B.

-----Original Message-----
From: Shepherd Madziwa [mailto:SMadziwa [at] psmi.co.zw]
Sent: Friday, 16 September 2005 8:39 PM
To: MYODBC [at] LISTS.MYSQL.COM
Subject: How to get data from newly added row in mysql database using
VB6.0

I have this nagging problem with my VB6.0 application. I am creating a
new record and I want to carry the record ID for future references down
the code. This record ID is AUTO_INCREMENT and I am using MYSQL
database. My code is as follows after the definition of my recordset and
initialising it:
rs.addnew
rs!lastname =3D text1.text
rs!firstname=3Dtext2.text
rs.update
varRecID =3D rs!ClientID
rs.close

Now when I trace the program "varRecID" is zero(0), but if I browse the
table in MYSQL I can see the record ID.

Where am I going wrong.

Thanks.


--
MySQL ODBC Mailing List
For list archives: http://lists.mysql.com/myodbc
To unsubscribe: http://lists.mysql.com/myodbc?unsub=3Dgcdmo-myodbc [at] m.gmane.o rg
jbonnett [ Mo, 19 September 2005 01:11 ] [ ID #973129 ]
Datenbanken » gmane.comp.db.mysql.odbc » How to get data from newly added row in mysql database using VB6.0

Vorheriges Thema: Apps that work with MyODBC
Nächstes Thema: Mapping from Boolean to Bit(1)