Access randomly displays records (No Duplicates) solution

Source: Internet
Author: User
Tags count
access| Solution | random | show | Read a lot of people talk about access to random records of posts, no, write a random display of records to solve, I hope you correct me. There are 5 records in the database, and 4 are randomly selected.

Code
--------------------------------------
 <% '-------------------------database connection-----------------------Set objconn = Server.CreateObject ("ADODB. Connection ") objconn.connectionstring =" Provider=Microsoft.Jet.OLEDB.4.0 "&_" Data source= "& Server.MapPath ("Data.mdb") objConn.Open '-------------------------database connection-----------------------'------------------------- Retrieving data-----------------------strSQL = "SELECT id,datacolumn from DataTable" SQL statement, retrieving database Set objRS = Server.CreateObject (" ADODB.                                                   Recordset ") ' Create recordset Objrs.open strSQL, objconn, 1, 1 ' Perform search Count=objrs.recordcount ' get total number of records item=4 ' Show the number of records '-------------------------retrieve data-----------------------'--------------------------- ----------------------------------------------------ReDim A (Item, 2), T (Count) ' defines 2 arrays, array A is used to store records, The array t is used to delete the record '---------------------------------------' initial array value, which is intended to be compared to this value after inserting the data for the each J-T j=0 Next '---------------------------------------'---------------------------------------' randomly extract record number Randomize TiMer ' initialize random number generator for j=1 to Item k=int (rnd*count+1) ' randomly take a record from the total. ' While T (k) <>0 ' determines whether the record is already The K=int (rnd*item+1) loop T (k) =1 ' K ' in the array is selected Next '--------------------------------------j=1:i=1 ' define subscript '---------           -----------------------------' Loop Select DataSet objRS part of the record in the array do objrs.eof if T (j) =1 then A (i,1) =objrs ("id") ' Record ID A (i,2) =objrs ("DataColumn") ' Records content i=i+1 End If j=j+1 objrs.movenext Loop '------------------------------- -------'-------------------------------------------------------------------------------'----------------------- -----Display Content--------------------for I=1 to Item Response.Write "Ordinal" &a (i,1) & "<br>" Response.Write "Content" & A (i,2) & "<p>" Next----------------------------display content--------------------'---------------------------' Releasing resources Objrs.closeset Objrs=nothingobjconn.closeset objconn=nothing '---------------------------%>


Data
ID    DataColumn--------------------------1        A2        b3        C4        d5        E


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.