Find Previous Record field data, or better suggestion

Anonymous
2020-01-13T19:45:54+00:00

I am trying to find the previous record value/field in a query to apply conditional formatting to my form. I have been researching using LAG function, but just can't make it work for what I need or am just not doing it right. 

What I have is a form that lists(and more): 

SERIES - TITLE

Series1 - Title1

Series1 - Title2

Series2 - Title3

Series3 - Title4

Series3 - Title5

Series3 - Title6

and I want to add a conditional format to hide the series values that repeat from the record above to show: 

SERIES - TITLE

Series1 - Title1

            - Title2

Series2 - Title3

Series3 - Title4

            - Title5

            - Title6

I tried to use the LAG Function, but dont think i'm adding it correctly.

Then i also found the below code but that just returns #NAME in the field. 

Function PrevRecVal(F As Form, Keyname As String, KeyValue, FieldNameToGet As

String)

End Function

    Dim RS As Recordset

On Error GoTo Err_PreRecVal

    'The default value is zero

    PrevRecVal = 0

    'Get the form recordset.

    Set RS = F.RecordsetClone

    'Find the current record.

    Select Case RS.Fields(ID).Type

        'Find using numeric data type key value?

        Case DB_INTEGER, DB_LONG, DB_CURRENCY, DB_SINGLE, DB_DOUBLE, DB_BYTE

            RS.FindFirst "[" & KeyName & "]=" & KeyValue

        'Find using date data type key value?

    Case DB_DATE

            RS.FindFirst "[" & KeyName & "]=#" & KeyValue & "#"

    Case DB_DATE

            RS.FindFirst "[" & KeyName & "]='" & KeyValue & "'"

    Case Else

            MsgBox "Error: Invalid key field data type!"

    End Select

        'Move to the previous record.

        RS.MovePrevious

        'Return the result

        PrevRecVal = RS(SeriesID)

Bye_PrevRecVal:

        Exit Function

Err_PrevRecVal:

        Resume Bye_PrevRecVal

        End Function

(in the control source of the field to show the previous record)

= PrevRecVal([Form],"SeriesID",[SeriesID],"SeriesID"

can anyone tell me 1)which one is better to use(above code or a lag function), 2)what im doing wrong or how to add a lag function if that is what i need here, 3)is there a better/easier solution you can suggest instead?Help! THANK YOU!

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2020-01-15T22:31:40+00:00

Let's confine it to one table to start with:

SELECT T1.SeriesID,

IIF(COUNT(*) = 1,T1.Title,"") AS Title,

IIF(COUNT(*) = 1,T1.SeriesPart,"") AS SeriesPart,

FROM TitleT AS T1 INNER JOIN TitleT AS T2

ON T2.SeriesID = T1.SeriesID

AND T2.Title <= T1.Title

GROUP BY T1.SeriesID, T1.Title, T1.SeriesPart

ORDER BY T1.SeriesID, T1.Title;

If that returns the correct result table then you can add the SeriesT table to the query and return the Series column rather than the SeriesID:

SELECT SeriesT.Series,

IIF(COUNT(*) = 1,T1.Title,"") AS Title,

IIF(COUNT(*) = 1,T1.SeriesPart,"") AS SeriesPart,

FROM SeriesT INNER JOIN (TitleT AS T1 INNER JOIN TitleT AS T2

ON T2.SeriesID = T1.SeriesID AND T2.Title <= T1.Title)

ON SeriesT.SeriesID = T1.SeriesID

GROUP BY SeriesT.Series, T1.Title, T1.SeriesPart

ORDER BY SeriesT.Series, T1.Title, T1.SeriesPart;

You can of course include the SeriesID and TitleID columns in the SELECT and GROUP BY clauses if you wish, but usually these would be surrogate keys with arbitrary values of no semantic significance.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

48 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-01-28T04:10:42+00:00

    Hi Moses, I've modified your dbase to use your tables and give the required output.

    I no longer answer questions on this forum. If you want to upload a text file to the same one drive folder with your email address I'll forward a fully working copy of the dbase and deal with you directly

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-01-28T00:09:25+00:00

    Would this fit the bill?

    SELECT IIF(COUNT(*) = 1,"Series: " & SeriesT.Series,"") AS Grouping,

    T1.Title, T1.SeriesPart

    FROM SeriesT INNER JOIN (TitleT AS T1 INNER JOIN TitleT AS T2

    ON T2.SeriesID = T1.SeriesID AND T2.Title <= T1.Title)

    ON SeriesT.SeriesID = T1.SeriesID

    GROUP BY SeriesT.Series, T1.Title, T1.SeriesPart

    UNION ALL

    SELECT IIF(COUNT(*) = 1,"Universe: " & UniverseT.Universe,""),

    T1.Title, T1.SeriesPart

    FROM UniverseT INNER JOIN (TitleT AS T1 INNER JOIN TitleT AS T2

    ON T2.UniverseID = T1.UniverseID AND T2.Title <= T1.Title)

    ON UniverseT.UniverseID = T1.UniverseID

    GROUP BY UniverseT.Universe, T1.Title, T1.SeriesPart;

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2020-01-27T21:20:12+00:00

    sorry i added a new one: 

    https://1drv.ms/u/s!AlB83s\_e-qI2ggTvdCw35HBMCuLW?e=dgght7

    On this I added the query TitleUnivGroup4Q that pulls all the info the way it should be listed from the separate tables, this just needs to have that grouping section added to the UniGroup field so only the first one is listed for that field like Grp in Form1.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2020-01-27T20:56:10+00:00

    ok guys, here's a test file I created to try and bridge the gap I am missing here. 

    https://1drv.ms/u/s!AlB83s\_e-qI2ggP2H4YNNkgnIftU?e=YxukjJ

    let me know if that link doesnt work... 

    The set up that SAMarshall provided was closest to what I want to achieve so I've been trying to work with this the most so far. The problem i'm still having issues is his qryMod calls for the items in the tblLink and that table has all the items in one table... however in my actual DB and exmapled in this testDB linked above, the TitleT only has TitleID, TItle, SeriesID, SeriesPart, UniverseID and UniverseOrder... the Universe and Series fields are linked by the ID numbers on their own respective tables. So how can I pull all the titles that have either a SeriesID or UniverseID on the TitleT? 

    Every way I've tried to create another query to call all the fields to one query list it will either only show those titles with BOTH (innerjoins) or all titles (rightjoin).

    So I'm thinking I either need another query to join all the information to one, or add to the FROM somehow on the qryMod SQL to pull Universe from the UniverseT and Series from the SeriesT as well as the other info already in that title table(tblLink aka TitleT).

    The Form1 form shows how SAMarshalls queries output the information the way I want to see it, and i have added the TitleT, SeriesT, and UniverseT... just need qryMod to pull data from those tables instead of the tblLink.

    Was this answer helpful?

    0 comments No comments