Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:18:04+00:00 2026-05-11T10:18:04+00:00

I have a query to return random distinct rows from an Access database. Here

  • 0

I have a query to return random distinct rows from an Access database. Here is the query:

SELECT * FROM  (SELECT DISTINCT m.MemberID, m.Title, m.FullName, m.Address,          m.Phone, m.EmailAddress, m.WebsiteAddress FROM Members AS m INNER JOIN MembersForType AS t ON m.MemberID = t.MemberID WHERE  (Category = 'MemberType1' OR Category = 'MemberType2')) as Members  ORDER BY RND(members.MemberID) DESC 

When I run this in Access it returns the rows in different order every time, as per the random sort order. When I run it through my web app however the rows return in the same order every time. Here is how I call it in my code-behind:

private void BindData() {     using (AccessDataSource ds = new AccessDataSource('~/App_Data/mydb.mdb', GetSQLStatement()))     {         ds.DataSourceMode = SqlDataSourceMode.DataReader;         ds.CacheDuration = 0;         ds.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;         ds.EnableCaching = false;                     listing.DataSource = ds.Select(new DataSourceSelectArguments());         listing.DataBind();         if (listing.Items.Count == 0)             noResults.Visible = true;         else             noResults.Visible = false;     } } 

I added in all that stuff about caching because I thought maybe the query was being cached but the result was the same. I put a breakpoint in the code to make sure the query was the same as above and it was.

Any ideas? This is driving me nuts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:18:04+00:00Added an answer on May 11, 2026 at 10:18 am

    When executing the ACE/Jet RND function against a new connection the same seed value is used each time. When using MS Access you are using the same connection each time, which explains why you get a different value each time.

    Consider these VBA examples: the first uses a new connection on each iteration:

    Sub TestDiff()    Dim con As Object   Set con = CreateObject('ADODB.Connection')   With con     .ConnectionString = _         'Provider=MSDataShape;Data ' & _         'Provider=Microsoft.ACE.OLEDB.12.0;' & _         'Data Source=C:\Tempo\Test_Access2007.accdb'     .CursorLocation = 3      Dim i As Long     For i = 0 To 2        .Open       Debug.Print .Execute('SELECT RND FROM OneRowTable;')(0)       .Close     Next   End With  End Sub 

    Output:

     0.705547511577606   0.705547511577606   0.705547511577606  

    Note the same value each time.

    The second example uses the same connection on each iteration (the .Open and .Close statements are relocated outside the loop):

    Sub TestSame()    Dim con As Object   Set con = CreateObject('ADODB.Connection')   With con     .ConnectionString = _         'Provider=MSDataShape;Data ' & _         'Provider=Microsoft.ACE.OLEDB.12.0;' & _         'Data Source=C:\Tempo\Test_Access2007.accdb'     .CursorLocation = 3      .Open      Dim i As Long     For i = 0 To 2       Debug.Print .Execute('SELECT RND FROM OneRowTable;')(0)     Next      .Close    End With  End Sub 

    Output:

     0.705547511577606   0.533424019813538   0.579518616199493  

    Note different values each time.

    In VBA code you can use the Randomize keyword to seed the Rnd() function but I don’t think this can be done in ACE/Jet. One workaround is to use the least significant decimal portion of the ACE/Jet the NOW() niladic function e.g. something like:

    SELECT CDBL(NOW()) - ROUND(CDBL(NOW()), 4) FROM OneRowTable 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 113k
  • Answers 113k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As far as I know, there is no direct way… May 11, 2026 at 10:05 pm
  • Editorial Team
    Editorial Team added an answer Do you have the button specified as an IBOutlet in… May 11, 2026 at 10:05 pm
  • Editorial Team
    Editorial Team added an answer Most likely, that one line should read: (vector-ref (apply access-nd… May 11, 2026 at 10:05 pm

Related Questions

I've been struggling with this one SQL query requirement today that I was wondering
I'm designing a website to teach myself how to use table joins in mysql
I want to show a random record from the database. I would like to
I have the following code, which will not work. The javascript gives no errors

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.