Friday, December 14, 2007

How to know if the current recordset can be updated?

How to know if the current recordset can be updated?

Normally, when the LockType of Recordset is adLockReadOnly, the record in the Recordset cannot be updated or modified. Another case is that the system is multi-user. If the LockType of Recordset is adLockPessimistic or adLockOptimistic, the system will not allow two-user update/modify the record at the same time.

So, the Recordset provides the Supports method to allow user to detect the current Recordset if it can be updated.



The ASP codes are written and described as below:

<%

Const adAddNew= &H1000400 'Use to detect the recordset can perform AddNew method or not.

Const adDelete = &H1000800 'Use to detect the recordset can perform Delete method or not.

Const adUpdate = &H1008000 'Use to detect the recordset can perform Update method or not.

Set conn = Server.CreateObject("Adodb.Connection")

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

Set rs = Server.CreateObject("Adodb.Recordset")

rs.open "Member",conn,1,3

If rs.Supports(adAddNew) Then

rs.AddNew 'Start Add new record

rs.Fields("MemberName")="Alvin" 'Assign the new value

rs.Fields("Location")="UK"

rs.Update 'Save the new data.

Else

Response.write("Cannot Add New Record")

End If

rs.Close

Set rs = Nothing

%>


No comments: