Results 1 to 13 of 13
  1. #1
    Scifi67 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Location
    West Sussex. UK
    Posts
    9

    Using data from 2 records in a report (specifying recordset record?)

    I'm not sure if I'm trying to do this in the correct/best way, I can't work out any other method for it...



    Basically, I'm trying to print a report with equipment details that also shows the current test date & the previous test date.
    I have a query that returns 2 records, the current test details & the previous test details (TOP 2 Records, based on the test date).

    My report is bound to this query, creating a 2 page report (all fields from the query are in the report).

    I need to produce a report that is only one page, containing all the data from record 1 (current) and just the test date from record 2 (previous).

    I would appreciate it if someone could point me in a reasonable direction, please...
    Do I need to manipulate the recordset in VBA (report onopen event)? Does anyone know of a comprehensive guide to manipulating recordsets, I haven't been able to locate one yet (maybe I just haven't found the right book).
    Or is there something I should do within the query (Dlookup, subquery)?

    I think I've confused myself by looking at too many possibilities at the same time & now can't remove the fog.
    I am not an Expert but do consider myself quite advanced, however, I haven't ventured too far in to VBA; except for plagiarising other peoples work (thank you to all those people ).

    Thank you for any assistance or ideas you may have.

  2. #2
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    A reasonable direction? Why certainly.

    Use the grouping feature of Access reports. The following window is taken from the Report Wizard dialogue but you may also invoke it (or something like it) for reports that are already designed.

    Click image for larger version. 

Name:	1.jpg 
Views:	27 
Size:	29.5 KB 
ID:	8749

    Assuming your records to be sorted correctly (and you can actually specify the sort sequence in the report design!) then let's assume you have attributes as follows:

    A
    B
    C
    X
    Y

    where A, B and C are the common data for two records and X and Y are the changing date. Simply ask Access Reports to group on A, B and C.

  3. #3
    Scifi67 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Location
    West Sussex. UK
    Posts
    9
    Rod,
    Thank you for the idea, not sure that will do the job though (I will investigate further though)...

    I hope you can see this (It is extremely small when I insert it), my query result is below, along with the affected part of my report.
    I need to use all the data from the 1st record & only the [TEDate] from the 2nd record, this needs to be applied to the blank field in my report "Previous Examination".
    I just can't work out how to refer to it in the text box control, or if there is a way I can combine the 2 records before the report gets opened?

    Click image for larger version. 

Name:	Query Result.jpg 
Views:	6 
Size:	15.8 KB 
ID:	8750


    Click image for larger version. 

Name:	Exam Report.jpg 
Views:	7 
Size:	43.3 KB 
ID:	8751

    Thanks again

  4. #4
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    No, in order to use my suggestion you must accept Access' report formatting which employs a type of 'cascading' approach. You on the other hand have a predefined report format suitable for humans rather than machines.

    Leave it with me for a while. There are basically two solutions. The first is that which you have already realised: merge the two records before attempting to print the report. The second is to only extract one record, the original, and base the report on this but look up the second record as you print each original record.

    Don't worry about attachment sizes. If you have used this forum's mechanism then it will expand when I click on it. Alternatively you may double click on it and choose a different attachment size.

    PS Tebay? Used to keep the banking engines for Shap at Tebay.

  5. #5
    Scifi67 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Location
    West Sussex. UK
    Posts
    9
    Thank you for taking the time to look at this

    'Banking Engines' 'Shap' that took some time to sink in... Like tug boats to help trains get over Shap summit?

  6. #6
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Here's a clunky prototype for look up at reporting time. I hope it's close to your own situation to be useful. The drawback is that the Format event only fires for print preview (and printing) so if you look at the report in report view the fields are blank. (Thanks MS! )

    MSAFTwoRecords.accdb

    There's some VBA code behind the report detail section for the on format event.

    I'll think about the way to merge prior to print and let you know.

    Yes, the banking engines resided at Tebay, went down the hill and where necessary assisted trains up Shap by pushing at the rear.

  7. #7
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    I have a 'cute' way of merging the data via VBA provided that a couple of prerequsites exist and an idea of a clumsy SQL solution. If you send me the table(s) and/or query(ies) definition (I don't need the data but it would help if not confidential) I shall send you both solutions. There you go: three for the price of one!

  8. #8
    Scifi67 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Location
    West Sussex. UK
    Posts
    9
    Rod,

    Sorry I haven't replied earlier, work hasn't allowed it today

    I've resorted to entering the data manually, for now, but I'd love to automate the process eventually.


    The whole database is 8mb+, I'm going to take it home & see what I can separate for you.
    I've also had some other thoughts, but I'll try to expand on those when my head isn't mush...

    Thanks,
    Dave...

  9. #9
    Scifi67 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Location
    West Sussex. UK
    Posts
    9
    Rod,

    You're a genius.

    I don't believe I would have got there on my own, no matter how much tinkering I did.

    Would it be possible for you to explain your solution on here; purely because you know what you did & I'm still trying to understand all the syntax.
    Although I've just realised, my problem was not the report, rather populating the form/table correctly; so this thread may be in the wrong forum list.

    From your directions, I have included the procedure in the form, locked & disabled the previous exam date field in my form and set the Serial before update event to call the procedure.

    My only question:
    Does the order of the string in strSQLDate matter? It doesn't seem to, but I have to ask or I won't learn.
    Your string builds as mm/dd/yyyy, my dates are dd/mm/yyyy, is this resolved at runtime (because of the DatePart "m" expression)?


    Thank you so very much.

    Dave...

  10. #10
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Yes the order mm/dd/yyyy is critical and that's the reason that there are those few lines exploding the date and reassembling it in the mm/dd/yyyy order. I guess the original SQL parsers were written in the USA and m/d/y is their date format. I have found using the string format '#mm/dd/yyyy#' the only reliable way of using date comparisons in Access SQL.

    Now Access actually stores dates as decimal numbers - the integer being a relative day number and the decimal part being so many fractions of a second past midnight - the real experts will tell you the precision. I think, I'm not sure (anyone?), that the SQL does a numeric comparison, so one second past midnight is greater than midnight; however you might be expecting the two dates to be equal forgetting that there is the time part attached. In fact when 'date only' comparisons are made against table date attributes we are assuming that the time part of those attributes is zero, not necessarily true. Allen Browne explains all this much better than me. Surf for his Access pages on the web.

  11. #11
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Regarding the explanation: yes I can do so but for which solution? There's the 'At print time' solution that I published here before I realised there was provision for a previous date on the source table, or there's the form/control 'On Current'/'Before Update' solution that I sent you privately. Then there's the system tool version.

  12. #12
    Scifi67 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Aug 2012
    Location
    West Sussex. UK
    Posts
    9
    The middle one: Control 'Before Update' event.

    I didn't want to be the only person to benefit from this

  13. #13
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : PopulatePreviousExamDate
    ' Author    : Rod
    ' Date      : 13/08/2012
    ' Purpose   : Retrieve, if any, the previous examination date and populate the correponding
    '             field. If the Serial field is null (e.g. a new record) then skip the look up.
    '             If the ID is null (e.g. current record has not yet been saved) look up the
    '             maximum date for this serial. Otherwise retrieve the maximum examination date
    '             excluding this record that is less than the date of this record.
    '---------------------------------------------------------------------------------------
     
    Private Sub PopulatePreviousExamDate()
        Dim strSQLDate As String
        Dim varExamDate As Variant
        If IsNull(Me.Serial) Then Exit Sub
        If IsNull(Me.ID) Then
            Me.Previous_Examination_Date = DMax("TEDate", "TblLoler", "TESerial = '" & Me.Serial & "'")
        Else
            varExamDate = Nz(Me.ExamDate, "X")
            If IsDate(varExamDate) Then
                strSQLDate = "#" & _
                             Format(DatePart("m", varExamDate), "00") & "/" & _
                             Format(DatePart("d", varExamDate), "00") & "/" & _
                             Format(DatePart("yyyy", varExamDate), "0000") & _
                             "#"
                Me.Previous_Examination_Date = DMax("TEDate", "TblLoler", "TESerial = '" & Me.Serial & "'" & _
                                                                          " AND TEDate < " & strSQLDate & _
                                                                          " AND ID <> " & Me.ID)
            Else
                Me.Previous_Examination_Date = DMax("TEDate", "TblLoler", "TESerial = '" & Me.Serial & "'" & _
                                                                          " AND ID <> " & Me.ID)
            End If
        End If
        
    End Sub
    It's not really rocket science but here goes. There are three controls of interest on the form:

    • 'Serial' which contains the key of the item (named TESerial on file),
    • 'ExamDate' which contain this instance's date (named TEDate on file) and
    • [Previous Examination Date] which contains the most recent TEDate before this instance.


    All three controls are bound to their respective columns in the underlying result set (table) via the Control Source property.

    I puposely designed the procedure for use upon the form's On Current event or the Serial control's Before Update event, so the first task is to make sure there is a non null value in the Serial control. If the value of Serial is null, which most likely happens when moving to a new record, then there's nothing to do so exit the procedure. (Code highlighted in red.)

    The next statement tests ID for being null. ID is an autonumber, the primary key of the bound table. There is no corresponding bound control so Me.ID directly interrogates the underlying result set. Autonumber primary keys are typically null when adding a new record. If the ID is null the blue code applies. However it is nigh on impossible that there will be a value in Serial while ID is null but I thought I had better cover the situation anyway. The blue code simply retrieves the latest date on file for this Serial using DMax. As serial is a text attribute it is necessary to enclose it in quotation marks.

    So now there is a record with a valid primary key (ID) and a non null value for Serial (TESerial). The next test is to determine whether the value in ExamDate is a valid date; it could be null (empty), some other rubbish or a valid date. Rather than use the IsNull function I use the trick of capturing the value of ExamDate in a variant (variant because I have as yet no idea what it is) while forcing the value to 'X' if ExamDate is in fact null. That's what the Nz function does: if the value of the first argument is null then use the second argument. (The green code applies.)

    The next statement tests whether the value of the variant can be interpreted as a date. 'X' cannot be a date which is why I chose it for the previous statement. This test is not foolproof however since numbers can be interpreted as dates. If the 'rubbish' I referred to in the previous paragraph happened to be a number then Access would be happy to believe it is a date. However if the control is defined and formatted for a date then this 'loophole' is unlikely to occur.

    If it's not a date the gray (grey for UK side of the Pond) text applies. This simply looks for the latest date for this Serial that is not this record. I have had second thoughts about this - see end of post.

    Otherwise the chunk of code in the middle applies. We have this record identified by ID; we have the Serial which identifies the item and we have ExamDate which specifies the date of the examination. The first few lines format the date suitably for SQL (see preceding post). It's then a matter of finding the most recent examination date for this item that is not this record and is earlier than the date on this record. Why the last condition? Well we can't be sure we are always dealing with the latest examination record.

    ---------

    Invoking this procedure from the form's On Current event does mean a slight performance hit but has the benefit of making the whole thing self-correcting in that, if in the interim, another record has been inserted between this and the former previous record, then the newly inserted record will be found.

    Second thoughts.

    I now think it better to set the previous date to null if this date is invalid. (Gray text.)

    Dave, I recommended that you should invoke this procedure before update of Serial. I now think that in addition it should be invoked before update of exam date.

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

Similar Threads

  1. Using a recordset to find ANY records
    By Drak in forum Programming
    Replies: 7
    Last Post: 01-10-2012, 07:11 PM
  2. Recordset not returning records
    By TinaCa in forum Programming
    Replies: 3
    Last Post: 08-03-2011, 09:26 AM
  3. Testing Recordset for Records
    By AccessGeek in forum Programming
    Replies: 5
    Last Post: 03-22-2011, 12:26 PM
  4. Replies: 1
    Last Post: 11-13-2009, 03:03 AM
  5. select records in recordset
    By sksriharsha in forum Programming
    Replies: 3
    Last Post: 09-05-2009, 11:40 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