Connecting Tech Pros Worldwide Forums | Help | Site Map

add a loop, next

Familiar Sight
 
Join Date: Jul 2006
Posts: 181
#1: Aug 17 '08
Hi, I know this is simple thing but always beats me.

I have this script that works fine, I just want to add a loop so that it returns all the entries instead of just one.

Expand|Select|Wrap|Line Numbers
  1. <% 
  2. ' ADO Constant. Dont change this 
  3. ' Connection string and SQL statement 
  4. Dim sql , connStr 
  5. sql  = "Select description  FROM tblGreetingPostCards"
  6. connStr = " Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("/database.mdb" ) 
  7. ' Opening database 
  8. Dim rs 
  9. Set rs = Server.CreateObject("ADODB.Connection")
  10. Set rs = Server.CreateObject("ADODB.Recordset")
  11. rs.Open sql, connStr, 3, , adCmdText 
  12. ' Generating random number from total number of records 
  13. Dim intRnd 
  14. Randomize Timer 
  15. intRnd = (Int(RND * rs.RecordCount)) 
  16. ' Now moving the cursor to random record number 
  17. rs.Movenext 
  18. ' Showing the random statement 
  19. Response.Write " <b><a class=""men"" href=""art.asp?art=" & rs("description") & """>" & rs("description") & " </a></b><br><br>"
  20.  
  21. ' Closing the database 
  22. rs.Close 
  23. Set rs = Nothing 
  24. %>
Any pointer would be great, I have tried adding loo and next just not sure of the right way.
Thanks
Richard

DrBunchman's Avatar
Moderator
 
Join Date: Jan 2008
Location: Winchester, UK
Posts: 930
#2: Aug 18 '08

re: add a loop, next


Hi Richard,

To loop through every record in the recordset use:
Expand|Select|Wrap|Line Numbers
  1. Do Until rs.EOF
  2.      'Do something with the record
  3.      rs.MoveNext
  4. rs.Loop
Is this what you mean?

Dr B
Familiar Sight
 
Join Date: Jul 2006
Posts: 181
#3: Aug 18 '08

re: add a loop, next


Thanks Dr B, worked a treat
Richard
Reply