I have an Access database application and I would like to know the proper way of decompiling and recompiling it.
I wrote a VBS script to automate the process of decompiling. It's silly that Microsoft hasn't integrated this into Access, considering it is a necessity when developing VBA-heavy applications.
The script locates MSACCESS.exe and runs Access with the decompile flag on a database located in the parent directory of the script, whose name is given in the code.
Option Explicit
Dim MSAccPath
Dim RegKey
Dim WSHShell
Dim currentDirectory
' Get MSACCESS.exe directory
RegKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\" _
& "CurrentVersion\App Paths\MSACCESS.EXE\Path"
Set WSHShell = WScript.CreateObject("WScript.Shell")
' Get parent directory
MSAccPath = WSHShell.RegRead(RegKey)
currentDirectory = WSHShell.CurrentDirectory
' Decompile
WSHShell.Run Chr(34) & MSAccPath & "MSACCESS.EXE" & Chr(34) & " " & Chr(34) & currentDirectory & "\..\testdb.accdb" & Chr(34) & " /decompile"
' Clear shell var
Set WSHShell = Nothing
Simply paste this text into a document with a .vbs
extension and double-click it to run. Access will launch, decompile the compiled P-code ("packed" code), and automatically recompile the VBA source back into P-code.
Just wanted to add my two cents. I work with SQL Server, SSIS and MS Access databases daily, and our network allows us to use different Citrix desktops; some are Win Serv 2003 SP2 and some are Win Serv 2008 R2 based. When you compact and repair MS Access dbs in one desktop the databases are fine. But, when you decompile, you have to make sure whoever else uses the dbs will be able to open them. Check all the controls to see if they work the same from workstation to workstation. You will run into issues when someone else's installation of MS Access doesn't contain the same libraries. That lead us to disallow all development of front-ends with unconventional controls, specially ones that you would have to add a reference to the db in order to use. Though, it's a great resource to bring size down and speed up your code, decompiling can cause tons of troubles in a multi-user environment
@Tim Lentine's practical instructions are good, but he leaves out the actual steps required for a decompile to be worth doing:
backup your database.
compact your database.
using the shortcut created with Tim's instructions, open your database.
close that instance of Access.
open a new instance of Access and open the database you just decompiled, but BE SURE YOU BYPASS ALL STARTUP CODE (i.e., hold down the shift key). If you don't do that, then you might as well go back to step 3 and try again, since if the startup code runs, your code will recompile before you are ready to do so.
compact the decompiled database (and be sure you hold down the shift key so that it bypasses the startup code; see #5).
open the VBE and on the Debug menu, choose COMPILE [name of project].
on the file menu, save the project.
compact again.
Why are all these steps necessary?
Because you want to not just decompile the VBA, you want to make sure that all the data pages where the compiled p-code was stored are completely discarded before you recompile.
I also recommend:
in VBE options, turn off COMPILE ON DEMAND
in the VBE, add the COMPILE button to your toolbar.
compile often with that button on the toolbar, after every two or three lines of code.
Decompile is not something you should use all the time, but during heavy-duty coding, I might do a decompile a couple of times a day. And I generally decompile/recompile as the last step before releasing an app into production use.
Last of all, read Michael Kaplan's article on the subject to understand it better.
To Decompile an Access database you'll need to create a shortcut with the following elements:
All together, then, the shortcut would look something like the following:
"C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" "C:\users\tim\documents\Mydatabase.mdb" /decompile
Obviously, the paths will be different on your system.
I'd recommend making a backup of your database before running this command.
If you have any startup code in your database you should hold down the shift key to bypass the startup code execution.
Once the database opens, you can compact and repair the database to ensure optimal performance.
After the compact and repair, you can recompile the VBA code by opening any module and using the Debug Compile [DatabaseName]
command.
If this is something you want to do frequently, you can create an "Access Decompile" shortcut in your SendTo Menu. Once you have this shortcut in the SendTo Menu you'll be able to right-click on any Access database and select "Send To --> Access Decompile", which is much easier than having to create a shortcut to the specific database.
Follow these steps to customize the Send To menu with an Access Decompile shortcut
Create a shortcut to the Access executable.
Append the /decompile flag in the Target for the shortcut. The shortcut will look like this:
"C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" /decompile
Open Windows Explorer and paste the following into the address bar:
%APPDATA%\Microsoft\Windows\SendTo
Copy the shortcut you created into the SendTo Folder.
The Access Decompile Shortcut will now be available for use.
To invoke the Access Decompile shortcut, right click on an Access Database in Windows Explorer and select "Send To --> Access Decompile". Be sure to hold the shift key down to bypass any startup code in the database.
The other answers here all seem a little complex to my taste.
To decompile an Access database:
Open the Run dialog from the start menu, or hit Win + R
Type in: MSACCESS.EXE /decompile
(a proper installation should open up the Access application, you could also provide the full path to MSACCESS.EXE), and press OK.
Access now opens. Open up your DB in the Access window that just opened. That will decompile it.
Or simply you can create a shortcut on your Desktop (with the /Decompile Flag) and drag drop your Access application holding SHIFT to bypass any code