Results 1 to 11 of 11
  1. #1
    Yoohoo is offline Novice
    Windows XP Access 97
    Join Date
    Nov 2010
    Posts
    1

    First function

    Hi all, i am new in this forum and as i am new to access database.
    i am trying to learn by reading tutorial online but some things are not clear and they dont present small sample data.

    for example, i am looking at the First function in access. i saw a query online such as



    select first(cid),
    cname,
    sum(usd),
    sum(pad),
    sum(cad)
    from <some table>
    group by cname

    i read that first function return the first value of a result set. but since i am new at this i am a little confuse since the tutorial does not give sample data. can somone explain what first function really does by illustrating some sample data. for example, you can post some sample data how it looks before the query is run and after the query is run using the query above. i trying to understand how this first function works. does the group by gets apply first then the first function or vice versa.

    please help me understand this. thanks in advance

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    This should explain how it works, and why we rarely use it:

    http://support.microsoft.com/kb/208190
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    I should have added that the group by is considered before the first, so that would result in the first cid for each cname.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    please keep in mind that first() returns an "uncertain" result which means you may not get the same result by running it this time and next time.

  5. #5
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by weekend00 View Post
    please keep in mind that first() returns an "uncertain" result which means you may not get the same result by running it this time and next time.
    umm....why is it uncertain?

  6. #6
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    umm....why is it uncertain?
    I am not sure how first() works.
    I believe it returns the field of first record that Access finds, and usually is the first record in physical storage position.

    for example, I create a table tblTest ( f1 text ) and type in 2 records in order:
    "result1"
    "result2"

    when I run "select first(f1) from tblTest", it returns "result1"

    if I delete the first record with text "result1", then add a new record, type in "result1" and run above mentioned query, it returns "result2".

    the table have the save data, but the results are different, that's what I mean "uncertain"

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    it's always certain weekend, but you have to know the order in which the records are stored. I believe that, if a table is indexed or ordered, it affects first and last() functions.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    It's quite predictable. From the link I posted in post 2:

    The First(), Last(), DFirst(), and DLast() functions ignore sort orders, indexes, and primary keys. These functions return the first or last undeleted record based on the order in which the records were entered into the table, not the first or last record in a specified sort order.

    Your results follow that.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    I see. but usually we don't know (don't care) the order of the records. therefore we can't predict what we will get.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Which is why I said

    Quote Originally Posted by pbaldy View Post
    This should explain...why we rarely use it
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    come to think of it, the only time I've even seen first() used in a query is with the 'find duplicates' built in query in access.

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

Similar Threads

  1. if function
    By lolo in forum Queries
    Replies: 1
    Last Post: 08-01-2010, 11:38 PM
  2. Want function to get current function name
    By Davis DeBard in forum Programming
    Replies: 2
    Last Post: 08-13-2009, 05:02 AM
  3. Sum Function Help
    By newbie in forum Reports
    Replies: 3
    Last Post: 06-30-2009, 05:32 PM
  4. Avg Function
    By hiker8117 in forum Access
    Replies: 3
    Last Post: 04-23-2009, 11:14 PM
  5. Is there a function to do this....
    By Nowherefast in forum Access
    Replies: 2
    Last Post: 12-31-2008, 08:08 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