Results 1 to 8 of 8
  1. #1
    timosilver is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    9

    Random row selection without selecting twice

    Hello,

    I have below statement that select row from my table at random.

    Code:
     sql = "SELECT TOP 1 * FROM Question1to5table ORDER BY Rnd(-10000000*TimeValue(Now())*[id])"
    how do make the row selection unique.



    :::: maybe mark a selected row on the dataset, if possible how do i do that ::::

    Thanks for help and idea.

  2. #2
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I think you would need a field in your table that indicated a record had been selected. Then you would have to update the record selected by your SQL, and add a where clause to the SQL to select only from unused records, for example:


    sql = "SELECT TOP 1 * FROM Question1to5table WHERE NOT Selected ORDER BY Rnd(-10000000*TimeValue(Now())*[id])"John

  3. #3
    timosilver is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    9
    thanks for your reply.. now i have added a 'Selected' field on my table.
    how do i update/mark the selected field?
    secondly i don't understand the 'John' at the end of the sql statement.

    Click image for larger version. 

Name:	ScreenHunter_01 Feb. 28 09.51.jpg 
Views:	13 
Size:	90.8 KB 
ID:	6505

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    See http://www.techrepublic.com/blog/how...oft-access/149 for more info on selecting random records.

    If you have to select say 20 random records, why not do it all at once? If you must do one at a time, then, in pure random selection, selecting the same record more than once may make sense.
    If you want one at a time, and no repeats, then carry on by ignoring records that have already been selected.

  5. #5
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    The "John" is my signature, not part of the statement - somewhere a line break got missed. Sorry for that bit of confusion.

    You can use two methods to mark the selected record, depending on how you use that SQL statement. If you make a recordset with it, just update the record. Assume the recordset is called rst:

    rst.edit
    rst!selected = True
    rst.update

    Or, you can use an Update SQL statement:

    SQL = "Update Question1to5table set Selected = True where ID = " & record_ID
    currentdb.Execute SQL, dbfailonerror

    where Record_ID contains the record number (ID)

    HTH

    John

  6. #6
    timosilver is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    9
    ok. thanks alot guys. really helpful

  7. #7
    timosilver is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    9
    i am using dataset is there a way to update selected field using the ds.

    Dim con As New OleDb.OleDbConnection Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String






    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = C:\Users\Timosoft\Documents\wwtbamDB.mdb"
    con.ConnectionString = dbProvider & dbSource


    con.Open()


    sql = "SELECT TOP 1 * FROM Question1to5table ORDER BY Rnd(-10000000*TimeValue(Now())*[id])"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "QuestionsandAnswers")

    con.Close()


  8. #8
    timosilver is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    9
    using data set can i do something like this
    to update the selected field at item 6 with 'True'
    ds.Tables("QuestionsandAnswers").Rows(0).Item(6) = "True"
    and then use the below sql

    sql = "SELECT TOP 1 * FROM Question1to5table WHERE NOT Selected ORDER BY Rnd(-10000000*TimeValue(Now())*[id])"

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Random Default Value Combo Box Selection
    By sking89 in forum Programming
    Replies: 2
    Last Post: 01-02-2012, 12:17 PM
  2. Random 0 now
    By Mounds in forum Access
    Replies: 4
    Last Post: 04-29-2011, 10:04 AM
  3. Selecting random with exclutions
    By owutne in forum Queries
    Replies: 1
    Last Post: 02-03-2010, 09:33 PM
  4. Random Rdn() Funtion Help Please
    By graviz in forum Access
    Replies: 1
    Last Post: 12-09-2009, 11:57 AM
  5. Random numbers
    By Gator777 in forum Access
    Replies: 3
    Last Post: 08-12-2009, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums