Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Ina is offline Novice
    Windows 10 Office 365
    Join Date
    Sep 2021
    Location
    Australia
    Posts
    23

    Retrieving the 2nd highest value from a set of records in a table

    I'm still very new in this. I manage to figure this out by looking at previous threads regarding 2nd value.
    I have a table called ReproductionT and I want a record where the WhelpingDate value is the 2nd highest.
    I have an Unbound Textbox in a subform called PrevWhelpingDate.


    This event runs from the subform PostPartumSubF.

    Private Sub Form_Load()
    DoCmd.SetWarnings True
    DoCmd.RunSQL "SELECT Max(WhelpingDate) AS PrevWhelpingDate FROM ReproductionT WHERE "" " & _
    "And MotherID = "" & MotherID AND WhelpingDate < (Select " & _
    "Max(WhelpingDate) FROM ReproductionT)"
    DoCmd.SetWarnings False
    End Sub

    I also have this in my main form:
    Private Sub Form_Load()
    PostPartumSubF.Requery
    End Sub
    Not sure if I have the right Event Procedures and how do I get the value to show in the textbox in the subform.
    It doesn't show a value when I open the form.

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,543
    Can you post a copy of the db.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,424
    surprised that actually works - there is a superfluous " after the WHERE

    SELECT Max(WhelpingDate) AS PrevWhelpingDate FROM ReproductionT WHERE " And MotherID = " & MotherID AND WhelpingDate < (Select Max(WhelpingDate) FROM ReproductionT)

    You also need to alias the table in the subquery

    (Select Max(WhelpingDate) FROM ReproductionT AS RT)

    I want a record where the WhelpingDate value is the 2nd highest
    for all records? which is what you are doing with


    (Select Max(WhelpingDate) FROM ReproductionT)

    or do you want the one for that particular MotherID which would require

    (Select Max(WhelpingDate) FROM ReproductionT AS RT WHERE MotherID=ReproductionT.MotherID)

  4. #4
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    UNTESTED

    You could do something like this: just make a function that looks up the top 2 dates and then returns the earlier of the two dates.
    Code:
    Private Function LookupPrevWhelpingDate(MotherID As Long)
    On Error GoTo ErrHandler
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim qry As String
        Dim rslt As Variant
        
        qry = "SELECT TOP 2 WhelpingDate FROM ReproductionT WHERE MotherID = " & MotherID & " ORDER BY WhelpingDate ASC;"
        
        Set db = CurrentDb
        Set rs = db.OpenRecordset(qry)
        
        If Not (rs.BOF And rs.EOF) Then
            rs.MoveLast
            If rs.RecordCount > 1 Then
               rs.MoveFirst
               rslt = rs!WhelpingDate
            End If
        End If
        
    ExitHandler:
        Set rs = Nothing
        Set db = Nothing
        LookupPrevWhelpingDate = rslt
        Exit Function
    
    ErrHandler:
        MsgBox Err.Description, , "Error #" & Err.Number
        Resume ExitHandler
    End Function
    And then set the control source of the textbox to something like =LookupPrevWhelpingDate(MotherID)

  5. #5
    Ina is offline Novice
    Windows 10 Office 365
    Join Date
    Sep 2021
    Location
    Australia
    Posts
    23
    Thank you for your reply, Bob. My zip file is 7mb which is too large to attach.
    I can give you access to my One Drive but I'm not sure if it's safe to do so. I don't want to make it available for anyone to access.
    Let me know what the safest way is to do this.
    I appreciate your help

  6. #6
    Ina is offline Novice
    Windows 10 Office 365
    Join Date
    Sep 2021
    Location
    Australia
    Posts
    23
    Hi kd2017
    I tried your function and I get an error no matter how I present it.
    I want the results for the Motherid that has been passed to the form. What should go in the brackets?

  7. #7
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    My zip file is 7mb which is too large to attach.
    1) Remove tables, queries, forms, reports not needed to solve your problem.
    2) test to make sure the necessary forms, etc. will run/open with what's left
    3) compact/repair and zip again.
    If it's still too large, you must have embedded images in there. Could delete those too.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  8. #8
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Until you can figure out how to upload your db file I'm afraid I can't be much more help. Here is an example db implementing the code I posted above (had to make a couple tweaks).
    example.zip

  9. #9
    CarlettoFed is offline Competent Performer
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2019
    Posts
    256
    Try this code:
    Code:
    Private Function LookupPrevWhelpingDate(MotherID As Long) as Date
        On Error GoTo ErrHandler
        Dim rs As DAO.Recordset
        Set rs = CurrentDb.OpenRecordset("SELECT WhelpingDate FROM ReproductionT WHERE MotherID = " & MotherID & " ORDER BY WhelpingDate")
        If rs.RecordCount > 0 Then
            rs.MoveLast
            If rs.RecordCount > 1 Then
               rs.MovePrevious
               LookupPrevWhelpingDate= rs!WhelpingDate
            End If
        End If
        
    ExitHandler:
        Set rs = Nothing
        Set db = Nothing
        Exit Function
    
    ErrHandler:
        MsgBox Err.Description, , "Error #" & Err.Number
        Resume ExitHandler
    End Function

  10. #10
    Ina is offline Novice
    Windows 10 Office 365
    Join Date
    Sep 2021
    Location
    Australia
    Posts
    23
    Dogs.zipHi kd2017, thank you for sending that example. I got it right. I have another field in the same table of which I need the value of. It is part of the same record which I'm getting the PrevWelpingDate from. I'm not sure how to do this as it is a number field, MLitterCount. The form is called PostPartumF
    I have a function I got from someone to work out current age. It works hundred percent on a single form but with a continuous form it calculates the first record and put the same value in all the other records. It could be just something small. The form is called UpcomingWhelpingF.
    It took me the whole day to reduce my db and I had to remove so much stuff to get under 2mb.
    Any help is very much appreciated 😍

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Seems like an overly complicated function to get an age.

    You are using an unbound textbox in your form. I believe you'll need to use a bound control and include the function in the forms source query.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I'm a bit confused. You say you want the latest date but you're ordering descending (latest first, earliest last) then moving last and getting that date. Would that not then be the earliest date that your function retrieves? To get the other value from the same set of records, add the field to your recordset and you'll be able to retrieve it. Not sure your db is completely normalized but I have to go out so I can't take the time to study it now.

    EDIT - I don't see why you have an IIF function in the control when you could just use the function that's generating the date. The function can deal with a null result and return anything you want if that is the case. However, your function should have a return data type and be more like

    Function LookupPrevWhelpingDate(MotherID As Variant) As theTypeYouWantHere

    The variant will allow you to process null. If you want to be able to return null, use Variant as the return type. Otherwise, you could type it as a string since you're currently using the IIF function to enter a zls (zero length string) anyway.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Ina,
    Something you might consider, if you select the top 1 of something, and then select the Top 1... where the value is not in (select the top 1...) you will get the second top record for the criteria.

    That is, if X is the Top 1 record, then selecting the Top 1 record from the table where the value is not in (X), you will get the second highest/latest record.

  14. #14
    Ina is offline Novice
    Windows 10 Office 365
    Join Date
    Sep 2021
    Location
    Australia
    Posts
    23
    Micron & Orange
    The code you refer to was given to me by someone else in this thread. I'm not having any luck getting the second value as I'm not yet very familiar with code.
    Could you please be so kind and look at my code and correct it for me, plse. The other value I need is the sum of Males+Females from the same record.
    Very much appreciated!
    I'm attaching a small version of my db.

    Dogs.zip

  15. #15
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I have a function I got from someone to work out current age. It works hundred percent on a single form but with a continuous form it calculates the first record and put the same value in all the other records. It could be just something small. The form is called UpcomingWhelpingF.
    See if this solves your unbound field in a continuous form problem.

    edit: am I missing something? It appears you have formatted dates as dd/mm/yyyy but in some instances they are appearing as mm/dd/yyyy.
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 10-09-2017, 11:36 AM
  2. Replies: 17
    Last Post: 08-28-2017, 06:25 PM
  3. Replies: 7
    Last Post: 05-23-2012, 02:19 PM
  4. retrieving a record from a table
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 04-25-2012, 01:22 PM
  5. Retrieving records according to criteria
    By jruizmesa in forum Forms
    Replies: 2
    Last Post: 03-24-2012, 11:43 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