I have a Microsoft Access database and there is a macro there. How do I view the code of the macro?
5 Answers
Open the Access Database, you will see Table, Query, Report, Module & Macro.
This contains the macros which can be used to invoke common MS-Access actions in a sequence.
For custom VBA macro, press ALT+F11.

- 33,172
- 3
- 63
- 88
-
5If ALT+F11 doesn't work, then try holding down SHIFT while opening the database. The "Use Access Special Keys" is probably unchecked in the current database's startup options. – Tim Lewis Jun 17 '14 at 18:42
-
1If you don't get a right-click menu for objects in the Navigation Pane (e.g. right-click the Macro and choose Design View), then the Startup Option "Allow Default Shortcut Menus" is probably unchecked in File -> Options -> Current Database. This is in Access 2010, I'm guessing that 2007 and 2013 have this option in a similar place. – Tim Lewis Jun 17 '14 at 18:58
You can try the following VBA code to export Macro contents directly without converting them to VBA first. Unlike Tables, Forms, Reports, and Modules, the Macros are in a container called Scripts. But they are there and can be exported and imported using SaveAsText and LoadFromText
Option Compare Database
Option Explicit
Public Sub ExportDatabaseObjects()
On Error GoTo Err_ExportDatabaseObjects
Dim db As Database
Dim d As Document
Dim c As Container
Dim sExportLocation As String
Set db = CurrentDb()
sExportLocation = "C:\SomeFolder\"
Set c = db.Containers("Scripts")
For Each d In c.Documents
Application.SaveAsText acMacro, d.Name, sExportLocation & "Macro_" & d.Name & ".txt"
Next d
An alternative object to use is as follows:
For Each obj In Access.Application.CurrentProject.AllMacros
Access.Application.SaveAsText acMacro, obj.Name, strFilePath & "\Macro_" & obj.Name & ".txt"
Next

- 31,973
- 6
- 70
- 106
-
1You know, when I tested that, I got a result that didn't include the commands within the macro, but just a collection meta-properties. When I run it now, it works. Your answer is better than mine, so +1. – David-W-Fenton Oct 15 '09 at 03:32
EDIT: Per Michael Dillon's answer, SaveAsText does save the commands in a macro without having to go through converting to VBA. I don't know what happened when I tested that, but it didn't produce useful text in the resulting file.
So, I learned something new today!
ORIGINAL POST: To expand the question, I wondered if there was a way to retrieve the contents of a macro from code, and it doesn't appear that there is (at least not in A2003, which is what I'm running).
There are two collections through which you can access stored Macros:
CurrentDB.Containers("Scripts").Documents
CurrentProject.AllMacros
The properties that Intellisense identifies for the two collections are rather different, because the collections are of different types. The first (i.e., traditional, pre-A2000 way) is via a documents collection, and the methods/properties/members of all documents are the same, i.e., not specific to Macros.
Likewise, the All... collections of CurrentProject return collections where the individual items are of type Access Object. The result is that Intellisense gives you methods/properties/members that may not exist for the particular document/object.
So far as I can tell, there is no way to programatically retrieve the contents of a macro.
This would stand to reason, as macros aren't of much use to anyone who would have the capability of writing code to examine them programatically.
But if you just want to evaluate what the macros do, one alternative would be to convert them to VBA, which can be done programmatically thus:
Dim varItem As Variant
Dim strMacroName As String
For Each varItem In CurrentProject.AllMacros
strMacroName = varItem.Name
'Debug.Print strMacroName
DoCmd.SelectObject acMacro, strMacroName, True
DoCmd.RunCommand acCmdConvertMacrosToVisualBasic
Application.SaveAsText acModule, "Converted Macro- " & strMacroName, _
CurrentProject.Path & "\" & "Converted Macro- " & strMacroName & ".txt"
Next varItem
Then you could use the resulting text files for whatever you needed to do.
Note that this has to be run interactively in Access because it uses DoCmd.RunCommand, and you have to click OK for each macro -- tedious for databases with lots of macros, but not too onerous for a normal app, which shouldn't have more than a handful of macros.

- 22,871
- 4
- 45
- 58
-
>> But if you just want to evaluate what the macros do, one alternative would be to convert them to VBA, which can be done programmatically thus: This is frustrating because you don't specify WHERE to do this. In some window? In Access? How? Where? Please be specific for the non-Access users who are trying to just figure out what some macro does. – noogrub Sep 29 '11 at 14:27
-
1You would paste the code into a public module within your Access database. But frankly, if you don't know that much about Access, you really shouldn't be doing anything at all like this, as you're not equipped to be doing it. – David-W-Fenton Sep 30 '11 at 18:59
This did the trick for me: I was able to find which macro called a particular query. Incidentally, the reason someone who does know how to code in VBA would want to write something like this is when they've inherited something macro-ish written by someone who doesn't know how to code in VBA.
Function utlFindQueryInMacro
( strMacroNameLike As String
, strQueryName As String
) As String
' (c) 2012 Doug Den Hoed
' NOTE: requires reference to Microsoft Scripting Library
Dim varItem As Variant
Dim strMacroName As String
Dim oFSO As New FileSystemObject
Dim oFS
Dim strFileContents As String
Dim strMacroNames As String
For Each varItem In CurrentProject.AllMacros
strMacroName = varItem.Name
If Len(strMacroName) = 0 _
Or InStr(strMacroName, strMacroNameLike) > 0 Then
'Debug.Print "*** MACRO *** "; strMacroName
Application.SaveAsText acMacro, strMacroName, "c:\temp.txt"
Set oFS = oFSO.OpenTextFile("c:\temp.txt")
strFileContents = ""
Do Until oFS.AtEndOfStream
strFileContents = strFileContents & oFS.ReadLine
Loop
Set oFS = Nothing
Set oFSO = Nothing
Kill "c:\temp.txt"
'Debug.Print strFileContents
If InStr(strFileContents, strQueryName) 0 Then
strMacroNames = strMacroNames & strMacroName & ", "
End If
End If
Next varItem
MsgBox strMacroNames
utlFindQueryInMacro = strMacroNames
End Function

- 6,570
- 9
- 45
- 63

- 21
- 1
-
Useful, but some false negatives when Access happens to split that part of the XML across two "comments" in the output file – Deanna Nov 18 '14 at 14:03
In Access 2010, go to the Create tab on the ribbon. Click Macro. An "Action Catalog" panel should appear on the right side of the screen. Underneath, there's a section titled "In This Database." Clicking on one of the macro names should display its code.
-
2The answer is not true. Clicking Macro on the Create ribbon creates a new blank macro. Clicking the macros under "In This Database" adds a "RunMacro" call to the new blank macro (the macro equivalent of a function/subroutine call). – Tim Lewis Jun 17 '14 at 17:52
-
After Clicking on Macro it should bring a new tab called "Design" click on Action Catalog so that the Action Catalog panel can appear from the right side of the screen if it had been closed – Emmanuel Njorodongo Oct 24 '20 at 20:41