Monday, December 17, 2007

How to add a record by using RecordSet

How to add a record by using RecordSet

S T E P - 1

- We need to create a RecordSet and use .AddNew method to add a new record.

Const adOpenKeyset = 1, adLockPessimistic = 2

Dim oConn

Dim oRs

Set oConn = Server.CreateObject("ADODB.Connection")

oConn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set oRs = Server.CreateObject("ADODB.Recordset")

oRs.Open "Member", oConn, adOpenKeyset, adLockPessimistic 'Open "Member"table

oRs.AddNew 'Start add new record, the record pointer will be point to the end of table

S T E P - 2

- Now you can assign the value to the table:

oRs("MemberID") = "Your ID"

oRs("Password") = "Your Password"

oRs("Email") = "Your Email"

S T E P - 3

- Finally, you need to update the table, if not, the new record will not be added into the database.

oRs.Update

S T E P - 4

- Dont forget close the RecordSet and Connection.

oRs.Close

Set oRs = Nothing

oConn.Close

Set oConn = Nothing


No comments: