Friday, December 14, 2007

How to use an optional method to move the current bookmark location?

How to use an optional method to move the current bookmark location?

ADO had provided another method to move the current bookmark location, which is:

.MoveFirst -Move to the first record.

.MoveLast -Move to the Last record.

.MoveNext -Move to the Next record.

.MovePrevious -Move to the Previous record.

Normally, How to use it?

For example:

<%

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

do while Not rs.EOF 'if the current bookmark location is not in the end of recordset, perform looping

If rs.Fields("Location") = "LA" Then 'When the Location fields is "LA"

Response.write(rs.Fields("MemberName")) 'Write down the member name

End If

rs.MoveNext 'Move to the next record to continue

loop

'The looping will stop, when the rs.EOF is TRUE; which mean the current bookmark location is point to the end of the recordset

rs.Close

Set rs = Nothing

%>

What will happen when we still need to use the "recordset" after looping?




The ASP codes are written as below:

<%

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

do while Not rs.EOF 'if the current bookmark location is not in the end of recordset, perform looping

If rs.Fields("Location") = "LA" Then 'When the Location fields is "LA"

Response.write(rs.Fields("MemberName") & " &nbsp &nbsp " & rs.Fields("Location") &"
")
'Write down the member name

End If

rs.MoveNext 'Move to the next record to continue

loop

Response.write(rs.Fields("MemberName") & " &nbsp &nbsp " & rs.Fields("Location") &"
")


'This will cause error !

'because, the current bookmark location is point to the illegal location.

rs.Close

Set rs = Nothing

%>



Below is the error message:

How to solve this problem?



The correct ASP codes are written and described as below:

<%

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

do while Not rs.EOF 'if the current bookmark location is not in the end of recordset, perform looping

If rs.Fields("Location") = "LA" Then 'When the Location fields is "LA"

Response.write(rs.Fields("MemberName") & " &nbsp &nbsp " & rs.Fields("Location") &"
")
'Write down the member name

End If

rs.MoveNext 'Move to the next record to continue

loop

rs.MoveFirst 'Move to the first record;

Response.write(rs.Fields("MemberName") & " &nbsp &nbsp " & rs.Fields("Location") &"
")

rs.Close

Set rs = nothing

%>

No comments: