Results 1 to 10 of 10
  1. #1
    chrisangk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    6

    Dynamic crosstab report sorting headings & dlookup

    Hi Guys,
    I am stuck with a tricky problem.

    I am currently working on a report that will show monthly trade position. I have to use the crosstab query view. I successfully implemented the already existing example of dynamic crosstab report (Hail to the man that wrote the code!).
    The problem I have been stuck on the last few days is how to automate column order based on months. The column headers are not in date format and there are couple of them that can't be converted to date format (like Inventory or Inventory in Transit). In order to tackle this difficulty I created a separate table with a month id key that gives number equivalent of the month. Now I want to sort the report dynamic headers according to this order.

    Dlookup function in the query didnt work for me. I am trying to use VBA dlookup, but when I select the criteria I get first row entry in all headers.


    Now I add couple of words on the workings:

    Report 605 delta is feed by qryCrossTabReport2, which takes data from 605DailyDelta crosstab query using a macro.
    Column heading in the report are taken from 605DailyDelta query
    SortOrder Table contains the months ID in the MonthID Field
    605DailyDelta column headers are based on MonthID Field from a query 605 tdelta (which is a union query it has a relationship with SortOrder table)


    I am trying to make work the dlookup, but if you have any other suggestion how to set the monthly order in headings I would gladly test them.

    I tried query sorting without much succes.

    I will appreciate any input.
    Thanks !



    Here's the code : ' Note when I run the report based on this i have the first row from my source in all the headers

    And for dlookup I replace the =fld.Name (that works fine but does not sort column headers)


    Option Compare Database
    Option Explicit
    Dim ReportLabel(22) As String
    Private Sub Report_Open(Cancel As Integer)
    Dim i As Integer
    For i = 0 To 22
    ReportLabel(i) = ""
    Next i
    Call CreateReportQuery
    End Sub
    Sub CreateReportQuery()
    On Error GoTo Err_CreateQuery
    Dim db As Database
    Dim rs As Recordset
    Dim qdf As QueryDef
    Dim fld As Field
    Dim indexx As Integer
    Dim FieldList As String
    Dim strSQL As String
    Dim i As Integer
    Dim MonthID As Integer
    Set db = CurrentDb
    Set qdf = db.QueryDefs("605DailyDelta")
    indexx = 0
    For Each fld In qdf.Fields
    'If fld.Type >= 1 And fld.Type <= 8 Or fld.Type = 10 Then
    FieldList = FieldList & "[" & fld.Name & "] as Field" & indexx & ", "
    ReportLabel(indexx) = DLookup("Trimmed", "SortOrder", "MonthID =" & MonthID) 'fld.Name

    'End If
    ' MsgBox Label(indexx)
    indexx = indexx + 1
    Next fld
    For i = indexx To 22
    FieldList = FieldList & "null as Field" & i & ","
    Next i
    FieldList = Left(FieldList, Len(FieldList) - 1)

    strSQL = "Select " & FieldList & " From 605DailyDelta"

    db.QueryDefs.Delete "qryCrossTabReport"
    Set qdf = db.CreateQueryDef("qryCrossTabReport", strSQL)

    'MsgBox strSQL

    Exit_CreateQuery:
    Exit Sub

    Err_CreateQuery:
    If Err.Number = 3265 Then '*** if the error is the query is missing
    Resume Next '*** then skip the delete line and resume on the next line
    Else
    MsgBox Err.Description '*** write out the error and exit the sub
    Resume Exit_CreateQuery
    End If
    End Sub

    Function FillLabel(LabelNumber As Integer) As String
    FillLabel = Nz(ReportLabel(LabelNumber), "")

    End Function

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    There is more than one way to stabilize pivoted data depending on the desired output.

    https://www.accessforums.net/reports...ast-30413.html

    http://www.tek-tips.com/viewthread.cfm?qid=1677633

    http://support.microsoft.com/kb/328320

    http://allenbrowne.com/ser-67.html

    http://forums.aspfree.com/microsoft-...ry-322123.html

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    chrisangk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    6
    Ok thanks I will do some reading. I added the database.
    I have found an interesting article
    http://stackoverflow.com/questions/1...ata-is-dynamic

    but maybe there is an easier solution.

    Thanks!!
    Attached Files Attached Files

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Which report - 605 delta? This is a very complicated process to generate this report. Just figuring out the source tables was a challenge.

    Why two identical tables - Idrepnew and Idrepold? Why all the blank fields and records? Why is text data preceded with a space?

    What date range should this report cover - 12 months plus the non-date subgroups?

    Part of the problem with the DLookup is that MonthID does not increment. It's value remains 0 throughout the procedure. Where is MonthID supposed to be picked up from? Try:

    ReportLabel(indexx) = DLookup("Trimmed", "SortOrder", "MonthID =" & indexx)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    chrisangk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    6
    Thanks June7. Your code works. Awesome!

    Btw do you know a way to shorten the width of the report, so it wouldn't show empty columns? It would automatically resize according to the data range. Is there any way to do it?
    I entered so many empty columns, because the data range may change every day going beyond the 12 months period and I dont know beforehand what will be the size of it.

    the report is to be produced on daily basis with new ldrepnew table and old ldrepnew being converted to ldrepold
    ldrepnew table (Current day database, workday)
    ldrepold table (Current day database -1, workday)

    The idea behind the report is to:
    1. Produce the trade position for the account in 605 pos based on ldrepnew table (Current day database )
    2. Produce the delta between ldrepnew & ldrepold (Current day database -1) (delta being the difference & new entries) and show in the report 605 delta
    3. Join both together in one report adding new entries list
    4. Make the process as error proof as possible.

    Both ldrepnew & ldrepold are extractions from a sql based database and imported to access via an excel file.
    Unfortunately the extraction from the main database is far from perfect, all the cells having empty spaces etc. This is something I can't change. The only thing I can do is to trim the cells in the excel file before importing to access.
    The file I posted is only there to do the design with a static database, because it works faster. The main file has a live link to the excel file where I copy the extracted data so it would update the access reports automatically, once I extract the data.

    I want to have new entries & delta showed in one crosstab table report, so I had to create 2 separate queries and then join them with a union query.

    Only writing about it is long. I know it is a very complicated process, but being new to Access (I started working on it only a week ago, before I hadn't even known it existed ) this is the best I could come up with.
    Do you know any way to simplify it ?

    Thanks !!!

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Okay, the blank fields make some sense but why all the empty records?

    Dynamically modifying the report would require complex code that opens the report in design view, determines how many fields with data (exclude the 'blank' fields) in the query, deletes/adds textboxes as needed, resets report width, and saves the revised report. Not an effort I would choose to tackle. Regardless of how many columns of data are in the query, the report has a maximum possible width of 22 inches. You could set the report to a fixed width (11-inch ??) then 'wrap' the records as multi-line rows of textboxes. Unfortunately, empty textboxes will still occupy vertical space.

    Refer to the last link in earlier post for another example of pivoting data. In that example the requirement is to print raw data records across (left to right) instead of vertically. The method allows for any number of records output to any number of pages. Each page actually prints as an independent report. This is not quite your situation except for the commonality of limited page width.

    There is usually more than one way to accomplish desired result but I don't have an alternative specific for your needs. I hope the examples provided offer enough insight for you to get what you want.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    chrisangk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    6
    Awesome stuff! Thanks. Appreciate your help! I think I will leave the empty space and find a smart layout to make it less noticeable. The monthly split must be presented horizontally, but it doesn't have to be printed out (pdf format's fine), so I will select the biggest paper size available (A2).

  8. #8
    chrisangk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by June7 View Post
    Which report - 605 delta? This is a very complicated process to generate this report. Just figuring out the source tables was a challenge.

    Why two identical tables - Idrepnew and Idrepold? Why all the blank fields and records? Why is text data preceded with a space?

    What date range should this report cover - 12 months plus the non-date subgroups?

    Part of the problem with the DLookup is that MonthID does not increment. It's value remains 0 throughout the procedure. Where is MonthID supposed to be picked up from? Try:

    ReportLabel(indexx) = DLookup("Trimmed", "SortOrder", "MonthID =" & indexx)
    Hi
    Unfortunetly I have a problem with ReportLabel(indexx) = DLookup("Trimmed", "SortOrder", "MonthID =" & indexx)It does not work as it should, it does not look up the month label, but just takes the following record from the Sort table ie. when I have 16 columns it will take first 16 records from the Sort order table. Is there any way to make it select monthly fields based on MonthID ?
    Thanks

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Try this:
    Code:
        For Each fld In qdf.Fields
            FieldList = FieldList & "[" & fld.Name & "] as Field" & indexx & ", "
            If IsNumeric(fld.Name) Then
                ReportLabel(indexx) = DLookup("Trimmed", "SortOrder", "MonthID=" & fld.Name)  'fld.Name
            Else
                ReportLabel(indexx) = fld.Name
            End If
            indexx = indexx + 1
        Next fld
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    chrisangk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    6
    Hi I haven't checked yet, but I have found a different solution. ReportLabel(indexx) = DLookup("Trimmed", "SortOrder", "MonthID=" & fld.Name) I used exactly that, but I changed the headings in the query to numeric values and it works well. Anyway your solution is simpler thanks for help!

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

Similar Threads

  1. Dynamic Headings on Multi Column Report
    By EddieN1 in forum Reports
    Replies: 7
    Last Post: 08-19-2012, 02:39 PM
  2. Replies: 2
    Last Post: 08-17-2011, 03:02 AM
  3. Replies: 1
    Last Post: 03-14-2011, 11:11 AM
  4. Sorting and Populating Report Headings
    By bpowers2010 in forum Reports
    Replies: 1
    Last Post: 08-11-2010, 05:05 PM
  5. Replies: 1
    Last Post: 07-30-2010, 10:28 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