Random record selection in Access

I need to define a record set in ASP / Access consisting of a
single record from the available records.
In PHP / MySQL I have used the following and it works fine:
SELECT *
FROM photolist
ORDER BY RAND()
LIMIT 1
Can anyone give me a suitable recordset definition to do this
in access?

"whatalotofrubbish" <[email protected]>
wrote in message
news:gmk6pe$90k$[email protected]..
>I need to define a record set in ASP / Access consisting
of a single record
> from the available records.
> In PHP / MySQL I have used the following and it works
fine:
>
> SELECT *
> FROM photolist
> ORDER BY RAND()
> LIMIT 1
>
> Can anyone give me a suitable recordset definition to do
this in access?
>
Access won't do a "random" command when queried from a web
page, although it
can if done so within the actual Access application. ASP
pages can certainly
utilize mySQL databases; why not use mySQL as it would be far
more robust
and stable than Access would be. If you can't move to mySQL,
try this
extension from DMXzone:
http://www.dmxzone.com/go?3705

Similar Messages

  • Random Record Selection

    I have a page that I have a recordset on just calling the
    RecordID in the database. How would I have the page or recordset
    select a random record and put that recordID in say a session
    variable?
    Thanks

    MySQL:
    SELECT yourfield FROM yourtable WHERE ... ORDER BY rand()
    LIMIT 1
    MSSQL:
    SELECT TOP 1 yourfield FROM yourtable WHERE ... ORDER BY
    newid()
    MS Access:
    SELECT TOP 1 yourfield FROM yourtable WHERE ... ORDER BY
    RND(yourprimarykey) [warning: this may not be random through
    CF]
    HTH
    Tim Carley
    www.recfusion.com
    [email protected]

  • Help with selecting 10 random records from all records meeting report selection criteria in Crystal 11

    <p>I am trying to select ten random records from all that match the report selection criteria then report on each of these random records for QA/QI.  I have tried the RND function however it is giving me a random number rather than a random record selection.  I cannot figure this out and am despirately seeking assistance.</p><p>Thank you,</p><p>Amy</p>

    <p>I don&#39;t know of any Random record selection functions but maybe you could write your own custom function inside of a record selection to randomly filter the records.  You would use the Rand function we currently have to decide if a record was included in the report data or not.</p><p>Another possible option is to filter the records before they get to the report.  You can only do this if you are pushing the data into the report instead of having the report pull the data.  An example of this would be passing an ado recordset to a report at runtime. </p><p>Rob Horne</p><p>http://diamond.businessobjects.com/blog/10 </p>

  • How can I select the random records in u201Cstep loopu201D.

    Hi, Experts,
    I am using step loop to display the data on screen now I want to select the random records from this step loop display. Like 1st 3rd or 5th record, is there any way to select the records,
    like I did in Table control there was a filed in internal table named marked char length 1, I gave this field name in u201Cw/ SelColumnu201D it fill that field with u2018Xu2019 from where I can get the info about the selected records, in this way I can easily perform the operation on that internal table with the help of that marked field.
    Is there any way to select the records in step loop too ?
    Kind Regards,
    Faisal

    thanks for replay shwetali,
    but i just gave you example of random records with 1st 3rd and 5th my goal is not select only these records. i want to select the random mean any records from the step loop display.
    like we can select from the table control. and when select any record it place 'X' in the given internal table field.
    Thanks and kind Regards,
    Faisal

  • Select Random Records with Group By?

    Hi,
    I currently select random records by using this type of SQL:
    (SELECT *
    FROM ( SELECT *
    FROM ( SELECT *
    FROM TABLEA
    ORDER BY dbms_random.value
    WHERE ROWNUM <= 100
    Based on the following data, I now want to still pick out random records but I only want 1 record from the same ID and START_DATE but where the FLAG could be Y or N.
    So with grouping by ID and START_DATE, I need to still be able to choose the record if it has anything from 1 entry or 10 entries per ID and START_DATE.
    Could this be performed with a GROUP BY?
    ID START_DATE FLAG
    1 12/12/2005 Y
    1 12/12/2005 N
    1 12/12/2005 N
    1 30/12/2005 Y
    1 30/12/2005 N
    1 30/12/2005 N
    2 10/10/2005 Y
    2 07/07/2005 Y
    Thanks for any help.
    Daniel

    Try this [not tested]:
    SELECT *
         FROM
              (SELECT *
                   FROM
                   ( SELECT
                              ID
                             ,START_DATE
                             ,FLAG
                             ,ROW_NUMBER() OVER
                                  (PARTITION BY ID,START_DATE ORDER BY dbms_random.value) RN
                        FROM TABLEA
                   ORDER BY dbms_random.value
         WHERE RN = 1
              AND ROWNUM <= 100
         ;

  • Selecting random records out of a table

    Hi,
    I guess this question isn't new, but I didn't found anything about this using the search of this forum.
    How can I get e.g. 5 random records out of a table?
    I have a small table, which stores the special offers of a shop. Let us say, there are 30 records in it. How can I fetch (e.g.) 5 random records of this table?
    I tried to use this statement in a loop:
    SELECT t_item_id FROM tbl_special_offers SAMPLE (1) WHERE ROWNUM = 1
    But this is a very small table, so most of the query returns no data. Additionally I have to check if this number was already selected and ignore it in this case. This isn't a good solution, is it?
    Can somebody help me out with a better solution? Thanks :)

    create or replace package shop_stuff
    is
       function random_number return number;
    end;
    create or replace package body shop_stuff
    is
       function random_number return number
       is
       begin
          return dbms_random.value;
       end;
    begin
       dbms_random.initialize (to_number(to_char(sysdate,'mmddsssss')));
    end;
    create table tbl_special_offers (t_item_id number);
    insert into tbl_special_offers values (1);
    insert into tbl_special_offers values (2);
    insert into tbl_special_offers values (3);
    insert into tbl_special_offers values (4);
    insert into tbl_special_offers values (5);
    insert into tbl_special_offers values (6);
    insert into tbl_special_offers values (7);
    insert into tbl_special_offers values (8);
    insert into tbl_special_offers values (9);
    insert into tbl_special_offers values (10);
    select t_item_id
      from (select t_item_id
              from tbl_special_offers
             order by shop_stuff.random_number)
    where rownum <= 5                       
    SQL> /
    T_ITEM_ID
             6
             7
             5
             4
            10
    SQL> /
    T_ITEM_ID
             1
             5
             3
             9
             6
    SQL> /
    T_ITEM_ID
             6
             5
             3
             2
            10
    SQL> /
    T_ITEM_ID
            10
             2
             8
             4
             1
    SQL>

  • Selecting Non repeatable random records

    HI all,
     I have 20 records in table. I want to get a random single  record from that table.And the main thing is
    if i having 20 records 1st time i'm getting a random record.the upcoming execution result that does not show previous random record .
    That is out of 20 records ,each time i want to select record that does not occur previously.so at 20 executions i have 20 result with non-repeatable.
    If i am confusing you,sorry for this.....pls help me to get a suffled record at each time
    note: i'm used NEWID() that gives random records but it occurs previously some time

    I cannot reproduce this :-)
    with
    [10] as (select 0 as c union all select 0),
    [11] as (select 0 as c from [10] as a, [10] as b),
    [12] as (select 0 as c from [11] as a, [11] as b),
    [13] as (select 0 as c from [12] as a, [12] as b),
    nums as (select top(20) row_number() over(order by c) as n from [13]),
    choice as (select top(1) n from nums order by checksum(newid()))
    select * from choice order by n 
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Display Random Record

    Morning all
    Just a quick one.
    How do I display a random record in DW?
    I have a MySQL statement which is:
    SELECT * FROM
    tablename ORDER BY RAND() LIMIT 0,
    number of records to display
    The error message I get is:
    'Syntax error (missing operator) in query expression RAND()
    LIMIT 0'
    If I alter 'RAND()' to 'RND()', I still get the error. If I
    remove the '0' I still get the error.
    Any ideas how to make this work?
    I'm using Access BTW.
    Regards
    Martin

    Tell your SQL statement what to RAND - right now it doesn't
    know how to
    randomize it, by what field.
    "Pantyboy" <[email protected]> wrote in
    message
    news:ef0and$r7g$[email protected]..
    > Morning all
    >
    > Just a quick one.
    >
    > How do I display a random record in DW?
    >
    > I have a MySQL statement which is:
    >
    > SELECT * FROM
    tablename ORDER BY RAND() LIMIT 0,
    number of
    > records to
    > display
    >
    > The error message I get is:
    >
    > 'Syntax error (missing operator) in query expression
    RAND() LIMIT 0'
    >
    > If I alter 'RAND()' to 'RND()', I still get the error.
    If I remove the '0'
    > I
    > still get the error.
    >
    > Any ideas how to make this work?
    >
    > I'm using Access BTW.
    >
    > Regards
    >
    > Martin
    >

  • Random record

    I'am looking for a way to randomly pick a record from an Access Database. I found this topic: http://forum.java.sun.com/thread.jspa?forumID=48&threadID=235843, but it isn't really what I am searching.
    The problem is that my primary key field isn't successive because of deleting and adding records... So using for example Random = 1 + random.nextInt(10000); to select a primary key will not work because there are numbers missing. Is there another way to randomly select a record?
    This is the code I use to connect with the database:
    package database;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import domein.Vraag;
    public class VraagManager {
         private final static String LEES_VRAAG_SQL = "SELECT * FROM vragen";
         Vraag vraagObject;
         public Vraag getVraag()
              Connection connection = Connectie.getInstance().getConnection();
    //           create Statement for querying database
              Statement statement;
              try {
                   statement = connection.createStatement();
                   // query database
                   ResultSet resultSet = statement.executeQuery(LEES_VRAAG_SQL);
                        resultSet.next();
                        String vraag = resultSet.getString("vraag");
                        String antwoord1 = resultSet.getString("antwoord1");
                        String antwoord2 = resultSet.getString("antwoord2");
                        String antwoord3 = resultSet.getString("antwoord3");
                        int correctAntwoord = resultSet.getInt("correctAntwoord");
                        Vraag vraagObject = new Vraag(vraag, antwoord1, antwoord2, antwoord3, correctAntwoord);
                   statement.close();
                   return vraagObject;
              } catch (SQLException e) {
                   e.printStackTrace();
              return null;
    }

    You are very helpful 8-) Anyway, I found a solution myself...
    Maybe I can help other people with this:
    Random randomNumber = new Random();
         int randomInt = 1 + randomNumber.nextInt(numberOfRecords);
         for(int i=0; i<randomInt; i++)     
                  resultSet.next();

  • Dynamic Table with Random Records

    What I am trying to do is select random records from a table
    and display them in a dynamic table with max columns set to 3 and
    the 4th record to be on a new row. Below is what I have right now
    and it works to randomly pick records but has no function to set
    columns in a table. If there is an easier way feel free to let me
    know. I have tried various ways to do this but none seem to work.
    <CFQUERY NAME="getItems" DATASOURCE="absi">
    SELECT catfit.*, modcats.*, prodmat.*, prod.* FROM catfit,
    modcats,
    prodmat, prod WHERE prodmat.prodid=catfit.prodid And
    catfit.catid=modcats.catid
    ORDER BY modl ASC </cfquery>
    <cfif getItems.recordCount>
    <cfset showNum = 3>
    <cfif showNum gt getItems.recordCount>
    <cfset showNum = getItems.recordCount>
    </cfif>
    <cfset itemList = "">
    <cfloop from="1" to="#getItems.recordCount#"
    index="i">
    <cfset itemList = ListAppend(itemList, i)>
    </cfloop>
    <cfset randomItems = "">
    <cfset itemCount = ListLen(itemList)>
    <cfloop from="1" to="#itemCount#" index="i">
    <cfset random = ListGetAt(itemList, RandRange(1,
    itemCount))>
    <cfset randomItems = ListAppend(randomItems, random)>
    <cfset itemList = ListDeleteAt(itemList,
    ListFind(itemList, random))>
    <cfset itemCount = ListLen(itemList)>
    </cfloop>
    <cfloop from="1" to="#showNum#" index="i">
    <cfoutput>
    <table width="205" border="0" align="left"
    cellpadding="0" cellspacing="0">
    <tr>
    <td width="235" height="116"> <div
    align="center"><img
    src="../Products/ProductPictures/#getitems.pic[ListGetAt(randomItems,
    i)]#" width="100"></div></td>
    </tr>
    <tr>
    <td
    class="ProdTitle">#getitems.brand[ListGetAt(randomItems,
    i)]# #getitems.modl[ListGetAt(randomItems, i)]#</td>
    </tr>
    <tr>
    <td
    class="paragraph">$#getitems.prc[ListGetAt(randomItems,
    i)]#</td>
    </tr>
    <tr>
    <td><A
    href="../Products/details.cfm?prodid=#getItems.prodid[ListGetAt(randomItems,
    i)]#" class="linkcontact">more
    info</a></td>
    </tr>
    <tr>
    <td> </td>
    </tr>
    </table>
    </cfoutput>
    </cfloop>
    </cfif>

    To start a new row after 3 records, do something like this.
    <table>
    <tr>
    <cfoutput query="something">
    <td>#data#<td>
    <cfif currentrow mod 3 is 0>
    </tr><tr>
    </cfoutput>
    </tr>
    </table>
    You should also know that your approach is very inefficient
    in that you are bringing in to cold fusion more data than you need.
    First of all you are selecting every field from 3 tables when you
    don't appear to be using all of them. Second, you are selecting
    every record and you only want to use 3. There are better ways out
    there, but they are db specific and you did not say what you are
    using.

  • Help with dynamic datasource and record selection

                                                                                                                                                                                                                                                                                     <span>Hi, I&#39;m having some difficulty with part of an application I&#39;m trying to build, but here&#39;s some background first.<br /> <br /> I am a student at the University of Maryland, and I&#39;m writing an application for one of the departments here at the university.  It is sort of a front-end for an access database with a lot of extra functionality.  This includes reports.  First of all, the user can change the databse that the application uses, so I store that in an appsetting.  For this reason, I have to tell the report what database to use at runtime rather than using the database expert.  I am building a sort of report wizard in which the user selects certain values, and based upon those values, the SQL statement that the report gets data from changes.  So almost everything about the report&#39;s data is dynamic and will be determined at runtime.  <br /> <br /> So for example if the user chooses an officer report by chapter names, I do this (chapters is a comma delimited string):<br /> <br /><span style="font-weight: bold"> string sel = "SELECT * FROM [Undergrad Leadership] WHERE [Leadership Position] = &#39;" + txtPosition.Text + "&#39; AND [Chapter Name] IN (" + chapters + ");";</span><br /> <br /> The part I am lost on, is how to actually interface with crystal reports.  In the following code, conn is an OleDbConnection to the correct database, rep is the ReportDocument, and view is a CrystalReportsViewer.<br /> <br /><span style="font-weight: bold"> rep.DataSourceConnections[0].SetConnection(conn.DataSource, conn.Database, false); <br /> rep.RecordSelectionFormula = sel;</span><br /><span style="font-weight: bold"> view.ReportSource = rep;</span><br /> <br /> When I try to load the report, I get the following error message:<br /> Error in formula <Record Selection>:  a number, currency amount, boolean, date, time, date-time, or string is expected here.<br /> <br /> Also, assuming this formula gets fixed, how do I actually get fields from this formula and datasource onto my report, since I can&#39;t do it at design time?  Thanks in advance for the help.<br /> <br /> -Jared<br />    </span>

    <p>RCAPI (Report Creation and Modification) calls are only available with Crystal Reports Server RAS SDKs.  This means that using the bundled version of Crystal Reports for Visual Studio will not allow you to place fields onto the report.  A free copy of Crystal Reports Server comes with a registered copy of Crystal Reports Developer.  My guess is that you don&#39;t have either of these products and that you are just using the product that came with Visual Studio.</p><p>All is not lost.  The first question is, do you really need to add the fields at runtime?  In many cases developers just want to have control of the data that gets sent to the report and they are ok with having the same fields display.</p><p>In your code you are changing the datasource which is fine, and you are assigning a Record selection formula which is also fine.  </p><p>The problem with the record selection is that it doesn&#39;t fit the syntax of the Report.  I would suggest printing out the value that you programatically get for the formula and insert it into the Crystal Report Designer.  You will probably get the same error there and gets some  hints as to why it doesn&#39;t work.</p><p>What I would suggest is creating a template report that already has the fields on the report and then change its datasource at runtime and add a RecordSelectionFormula.</p><p>Otherwise you will have to use RAS to be able to add fields at runtime. </p><p>Rob Horne<br /><a href="/blog/10">Rob&#39;s blog - http://diamond.businessobjects.com/blog/10</a></p>

  • Random record return

    I have a DB of sites that I need to query and pull in 5
    records that are to be displayed in a random order.
    I can limit the records but I'm struggling with displaying
    them in a random order.
    Any ideas?

    We're using MySQL as the DB and to clarify I need to pull in
    5 random records in a random order.
    I've just been emailed the answer by a friend so I'llpost it
    up here.
    "SELECT *
    FROM tablename
    WHERE somefield='something'
    ORDER BY RAND() LIMIT 5"
    Seems to work fine to me.

  • Copying records from MS Access to Oracle ODBC

    I have a linked table from Oracle to an MS Access DB table. I am trying to "upsize" many records from the original MS Access DB table to one mirrored in Oracle.
    All is well save for the currency field in the original MS Access table. If I change the field to number or something from currency in Access, I will lose the formatting and potentially the data.
    I've tried several number combinations in the Oracle table but while I can copy and paste the records from the Access table to the linked Oracle table without getting any errors, when I query the records in the Oracle table it just shows pounds signs.
    Is there a certain number format in Oracle I should use for this?
    Is there a definitive way to make that work?
    I'll take any feedback I can get on this.
    Thanks.

    ok, this is still a bit insane. I copied all the records over, and in the MS Access ODBC linked part, I see the numbers correctly.
    After running various queries in Oracle, all are still showing #s unless it's a 0 value.
    i.e.
    SQL> SELECT LOAN_BALANCE FROM SEPTEMBERUPLOAD1A WHERE AUTOID < 30;
    LOAN_BALANCE
             .00
             .00
    29 rows selected.So then the question is, while I assume the data is in there potentially, and correct at that, is there a formatting trick I need to apply when querying the data through Oracle? Will people just see the pound signs? Let's say I'm later writing a Perl (yuck!) or Java (yeaaaah!) app on the web to view the data, will people see # signs? Or will they see double/decimal type figures?
    never mind....I guessed using the TO_CHAR function to retrieve it...I see that's what you do.
    SQL> SELECT TO_CHAR(LOAN_BALANCE) FROM SEPTEMBERUPLOAD1A WHERE AUTOID < 30;
    TO_CHAR(LOAN_BALANCE)
    0
    27786.16
    19864.43
    123670.56
    38820.51
    1417502.28
    101465.81
    90724.64
    127566.76
    62615.79
    74805.47
    17925.83
    122796.58
    103719.01
    72914.53
    169919.59
    33181.53
    0
    29623.22
    34192.74
    82956.63
    14387.6
    37506.27
    78149
    56214.33
    116728.95
    62696.78
    94718.46
    48605.89
    29 rows selected.Thanks!
    Message was edited by:
    user515689
    Message was edited by:
    user515689

  • Random Record from a Query

    I have a page on my site that displays certain information in
    kind of a "feature" section, where it needs to query the database
    based on certain criteria and then display the data for just one of
    the records. The record should be different each time the page
    loads. So, if my query has 10 or 20 records, I just want to select
    one of them randomly and disply its data. What would be the best
    way to code such a scenario?
    Many thanks.

    first, a statement that contains "The record
    should be different each time the page loads" and "select one
    of them
    randomly and display its data" is a bit of a contradiction:
    you either
    display a random record or you control which record to
    display to make
    sure each page load displays a different one.
    if you just want a random record displayed (which means same
    record may
    be displayed on subsequent page loads) then how to do it
    depends on
    which database you are using, as cfSearching pointed out.
    here's a great place to look to sql statements for this for
    various db
    systems:
    http://www.petefreitag.com/item/466.cfm
    if you want to display a different record each page load, you
    need to
    keep track which records have already been displayed
    previously and
    display the next one in sequence. you could do this by
    storing the list
    of record ids in an application-scope variable, displaying
    the record
    with the first id and removing that id from the list of ids.
    when the
    list is empty, query the db for record ids again and make a
    list anew.
    hth
    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/

  • Controlling Record Selection for Subreports from the Main Report

    Post Author: Horizon57
    CA Forum: Formula
    Hello - I am generating a standard report template that will be applied to all of my accounts and I am connecting to an Access DB that has multiple tables. Each table corresponds to a single Subreport within my Mainreport and contains the data for all of the accounts. Essentially I would like to be able to write some logic in the Formula section of my Mainreport whereby I can control which account to run the report for based on the Account Name.  I am having sucess with this method when I change the parameters of the formula in each subreport, but how do I modify my code to allow me to control this from the mainreport.  An example of a formula is below. // Account AAA//{Subreport1.Account } = "AAA" and {Subreport2.Account } = "AAA" and {Subreport3.Account } = "AAA" and {Subreport4.Account } = "AAA"// Account BBB//{Subreport1.Account } = "BBB" and {Subreport2.Account } = "BBB" and {Subreport3.Account } = "BBB" and {Subreport4.Account } = "BBB"// Account CCC//{Subreport1.Account } = "CCC" and {Subreport2.Account } = "CCC" and {Subreport3.Account } = "CCC" and {Subreport4.Account } = "CCC" Essentially, I am looking to just uncomment the particular Account I would like to produce the report for and have that filter down to all the subreports. How do i Modify the logic? Is there a better way to approach this?

    Post Author: Horizon57
    CA Forum: Formula
    Thanks for all the responses..... Just to clarify, I am essentially trying to control the record selection of each subreport with a forumula in the mainreport. To this end I am trying to avoid having to modify the formula in each subreport to change accounts each time i need to run a set of reports. I am still not having any luck modifying the subreport links as mentioned above. The only way I have been able to control the data is with formulas in each subreport. If anyone has accomplished what I am attempting, could you post an actual formula from both the subreports and mainreport so I can try to mirror them. Also, if anyone can point me to an example where I can integrate a dialog box to automatically control the record selection (as mentioned above), that would be great. Thanks,Mike

Maybe you are looking for