Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Record from Access DB (Using Query)

Status
Not open for further replies.

nevets2001uk

IS-IT--Management
Jun 26, 2002
609
GB
Originally posted in the ASP.NET forum I have the following problem...

If am working from an Access Database and would like to pull a recrod at random from my database to display in a random photograph section of my new website. I had this working in SQL but had to move back to access for hosting at this time.

Here is what I've got based on a few google searches. The problem I have is that although requerying the table does cycle through different photographs it doesn't do it randomly and I always get the same sequence of photos.

SELECT TOP 1 PHOTOGRAPH_ID, RND([PHOTOGRAPH_ID]) AS RANDOMID, GALLERY_ID, TITLE, REFERENCE FROM PHOTOGRAPHS ORDER BY Rnd([PHOTOGRAPH_ID]) DESC

Any thoughts how to do this? The query is inside a table adapter which might make a difference?


Steve G (MCSE / MCSA:Messaging)
 
It is the bit:

RND([PHOTOGRAPH_ID])

RND is not truly random - supplying the same seed will always return the same number, and a photograph ID is fixed, you get the same top record.

Try using something else to randomise it, that changes all the time. how about

RND(now()+[PHOTOGRAPH_ID])

As now() (the current date/time) is always changing, the random order alwas changes. Note Now() on it's own will not work as then all records would get the same random number.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top