Results 1 to 5 of 5
  1. #1
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104

    Getting greatest number based on conditions from records using DAO

    I searched and couldn't find anything, but perhaps I'm just inept at searching.

    The fields I have that are relevant are:
    Section - Number, Long Int
    Period - Text
    DepNum (deposit number) - Number, Long Int


    I wanted to be able to have a certain set of conditions to find out the next deposit number by opening a recordset on the data (tblDeposits table).

    Say Section "1" is in the "November 2010" period (financial month) with DepNum 1, 2 and 3. I wanted to make the "on lost focus" event for the cbo selection for Section look up the greatest number for the period and section (hidden text box), so I can then add 1 to it to get the next deposit number (in the aforementioned example, the text box would have "3" in it so I could then add 1 to it and have DepNum4 as the next deposit).

    I assume if there are no deposits (say for the first deposit of the period), I can use like:

    Me.txtDepNum = Nz(rs!DepNum, "1")



    to specify.

    As always, any help is much appreciated by the guy that's always poking people for help!

    Justin

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Justin,

    people do this sort of thing by using dmax() and dcount(). eg. -
    Code:
    me.tbox = iif(dcount("field", "table") = 0, 1, dmax("field", "table") + 1)

  3. #3
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    Okay, that makes sense, but how would you do it given those constraints? I see where it would return the greatest number in the field, but there will be 3 sections using the db with different deposit numberings, plus it needs to read only the current period (stored in another table, but on load txtPeriod is filled with that stored value on the same form so each added item into tblDeposits has the field for Period).

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    read the help article on those functions. they give the syntax. e.g., they have 3 parts:
    Code:
    (field, table, criteria)
    remember that MS documentation specifies optional code and syntax by enclosures in [].

    example - look up max dep number for last month, the mo of november, and give appropriate NEW dep number:
    Code:
    =iif(dcount("deposit", "table", "[datefield] > #10/31/2010#
    
    AND [datefield] < #12/1/2010#") = 0, 1, 
    
    dmax("deposit", "table", "[datefield] > #10/31/2010#
    
    AND [datefield] < #12/1/2010#") + 1)
    does that make sense?

  5. #5
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    Yes it does, thank you very much for your help!

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

Similar Threads

  1. Select Random Records Based on Conditions
    By access123 in forum Queries
    Replies: 1
    Last Post: 10-27-2010, 10:25 AM
  2. Insert x number of rows based on value
    By opopanax666 in forum Programming
    Replies: 4
    Last Post: 10-26-2010, 03:26 AM
  3. Replies: 1
    Last Post: 03-05-2010, 12:27 PM
  4. Query result based upon two field conditions
    By diane802 in forum Access
    Replies: 35
    Last Post: 01-08-2010, 06:31 PM
  5. Replies: 5
    Last Post: 10-08-2009, 05:15 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