Dynamic Reporting in Access

Posted on 2009-04-28
Last Modified: 2013-11-27

I am trying to do a dynamic reporting program in access.  The scope is there are two main tables Staff and Projects.  These tables have a lot of joins to other tables.  The user needs to be able to view the Field names in these tables and choose which fields they would like to see in a report (this is being exported into excel).  I have a form which displays staff members so they pick the staff member or members (this is stored in global variables).  A form then displays a listbox with a list of the fields available from the tables they have chosen.  I have then written code to put the field names and corresponding table names into a table.

I am now trying to write code to build a SQL statement to get the data they require using the staff ids stored in variables and then the field and table names stored in a table.  I will then need to write in the joins and maybe create a table containing all this info to export to excel.

Does anyone have any suggestions on this last bit?  I am on a very tight deadline (end of week!) and I am working from home whilst looking after a baby so finding it all quite hard!

Thanks so much in advance
Question by:WhatwasIthinking
    LVL 1

    Expert Comment

    Hi, how much SQL Server experience do you have?

    you will probably need to use the various information schemata schemas, or the systables and sysobjects schemas, to get the table and column names, and then use dynamic SQL on the server to run the queries. An example is:
    from sysobjects so
    INNER JOIN syscolumns sc ON =
    WHERE  'Employees'
    select column_name from INFORMATION_SCHEMA.COLUMNS
    WHERE  table_name='Address'

    This will list the column in employees or the address table.
    Note however that your queries can always return more columns that the user specified and then get Access to not display those columns, the complicated part is in the where condition - when there are different data types -eg dates, integers and strings.
    Also you can use SQL views for commonly needed reports which would already have much of the formatting and columns you require.

    LVL 75

    Expert Comment

    by:Anthony Perkins
    >>I am trying to do a dynamic reporting program in access.<<
    Has this got anything to do with SQL Server?
    LVL 74

    Accepted Solution


    While what you are asking can certainly be done, it may not be considered "Easy" for a beginner.

    So again, you must have a good working knowledge of MS Access, VBA, SQL, Query Design and Form design.

    If this is really crunch time, then you may wish to simply have the users build the queries themselves, selecting the fields they want.
    Then provide a combobox to list which queries they want to export.
    The write the export code to reference the combobox value
          DoCmd.TransferSpreadsheet acExport, , Me.cboQuery, "C:\YourFolder\Export.xls", True

    Here is a sample.


    Author Comment

    thanks for all your tips.  i have now completed this project with a combination of vbscript and sql queries.
    thanks again
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    Isn't that what jmreidy suggested and posted an example of?
    LVL 1

    Expert Comment

    I don't think this was SQL server related, the author mentioned SQL, but not SQL server.
    It sounds like the solution was MS Access related.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In the previous article, Using a Critera Form to Filter Records (, the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now