Documenting Access Queries in Excel

October 26, 2009 by datapig Leave a reply »

Clients usually have this pesky need to have everything documented. As a generally lazy person, this offends my lackadaisical attitude. Nevertheless, I typically oblige, given the fact that I want them to take over the work at some point.

Over the years, I've rolled my own documentation tools to help in this endeavor. Today, I want to share a small procedure I use to document Access queries in Excel.

Copy this code into a standard module in Access.

Visual Basic:
  1. Function Document_My_Queries()
  2. Dim TempTable As DAO.Recordset
  3. Dim QryDef As QueryDef
  4. Dim QryName As String
  5. Dim strSQL As String
  6.  
  7. 'Step 1: Start with a fresh QueryList table
  8. On Error Resume Next
  9. docmd.DeleteObject acTable, "QueryList"
  10. CurrentDb.Execute _
  11. "CREATE TABLE QueryList (QueryName text(100), DateCreated Date, LastUpdated Date, SQL memo)", dbFailOnError
  12. On Error GoTo 0
  13.  
  14. 'Step 2: Open the QueryList table
  15. Set TempTable = CurrentDb.OpenRecordset("QueryList")
  16.  
  17. 'Step 3: Loop through all queries and fill the QueryList table with Query Def info
  18. For Each QryDef In CurrentDb.QueryDefs
  19. 'Exclude deleted querys
  20. If Left(QryDef.Name, 1) = "~" Then
  21. GoTo SKIPIT
  22. End If
  23. TempTable.AddNew
  24. TempTable!QueryName = QryDef.Name
  25. TempTable!DateCreated = QryDef.DateCreated
  26. TempTable!LastUpdated = QryDef.LastUpdated
  27. TempTable!SQL = QryDef.SQL
  28. TempTable.Update
  29. SKIPIT:
  30. Next
  31.  
  32. 'Step 4: Use TransferSpreadsheet to export to Excel
  33. docmd.TransferSpreadsheet acExport, 8, "QueryList", _
  34. CurrentProject.Path & "\MyQueries.xls", True, ""
  35.  
  36. End Function

In Step 1, you create a fresh QueryList table. This table will hold the info about your queries. If a QueryList table already exists, it gets blown away and replaced by a fresh one.

In Step 2, you open the QueryList table into a recordset.

In Step 3, you loop through all the queries in the database (skipping the ghost queries that start with ~) and you log the needed info into the QueryList table.

In Step 4, you use the TransferSpreadsheet method to send the QueryList table to Excel. TransferSpreadsheet will allow the ultra-long SQL statements to survive Excel's 255 character limit for a cell. Meaning, the SQL statements won't be cut off at 255 characters.
The workbook will be saved into a file called MyQueries.xls in the same directory as the host database.

Here's what the final spreadsheet looks like.

Why send this info to Excel? I find that an Excel workbook makes for a nice documentation file, allowing me to partition different aspects of my documentation into tabs. Queries on one tab, Tables on one tab, Forms on one tab, etc.

Outside of documentation, you can also use this to essentially create a backup of the queries in your database.

Advertisement

7 Responses

  1. Tony Rose says:

    This is a good suggestion Mike! Along similar lines for a non-technical person and casual user of Access is to document steps in a table in Access.

    I recently created a bunch of queries and tables. I used some standard naming conventions to make the queries appear in order, but not all of the info can be logged in a query or table name.

    To log my steps and criteria, I created a table called "Notes" and opened the field up to more than 50 characters. Then I simply typed into a table the steps and criteria I used to build my final table for the analysis. This allows anyone to go into the database and view the steps and criteria used without having to look at each query and table.

    Along the same lines as your post, so I just wanted to share.

  2. Fred Chidester Sr says:

    Ok great idea in fact it could not have come a better time. Just finished a small Access app and need to give a break down of everything I have within the database. Like the kids say today - du - could you give one more step to where to place this code (2007 Verison) Objects I no longer see modules listed or am I missing something?

  3. datapig says:

    Fred: In Access 2007, go to the Create tab and click the Macro dropdown. There, you can choose Modules. This will create a new module for you. Place the code there.

  4. Fred Chidester Sr says:

    YES!! Mike Your A Genius It Work Perfectly That is what it all about learning from the Pro simple little things. PS in todays comics Dagwood trys Bacon Ice Cream Great Yes Yes Yes. Thank-you

  5. Jody MacKenzie says:

    You are my hero! Thank you. So much better than printing all that one-object-per-page stuff in Access' Documenter.

  6. Professor says:

    Hi,

    In your listing of the VBA code, line 34: replace & with &

    Works great. Thanks.

  7. Professor says:

    Hi,

    Line 34 should be just the & instead of the HTML equivalent when you paste it to Microsoft Access.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>