Access Development Tips

Table Topics | Queries and Recordsets

Form Tips | Report Topics| Module Code and APIs

General

Open a Database with any version of Access from the  Right Click context menu

Add a Compact Database Option to your Right Click Menu.

How to Automatically compact your Access dbs on a scheduled basis.


More to Come…. .



















Back to the Tips List

How to Add the Option to Your Right Click Context Menu to Compact an Access Db with any version of Access on your computer.
To add a compact option to your right click context menu, you can edit the file type in the File Type in the Windows Explorer Folder options dialog or you can edit your registry directly.

To add the option via Windows Explorer, go to the folder options from the menu and select the file type tab.  

1.) Find the MDB (mda, mde) file type and click "Advanced"

2.) Click the New button and add the name of the command in the Action option you want to show up in the context menu such as "Compact".

3.) Browse to the location of msaccess.exe

4.) After the path has been entered in the Application option add the following "%1" /compact

If you want to manually edit the registry on your computer in Windows 95/98 and NT 4/2000:

1.) Make sure you have a back up copy of your registry, in case of an error.

2.) Open the Registry Editor, and under the key "Hkey_Classes_Root", find the key "Access.Application.X". Where X = the highest version of Access you have installed.

3.) Under this Access.Application.X key click on the "Shell" Key. While Shell is highlighted, use the edit menu item to Add . NewKey. Call this new key "Compact".

4.) Highlight the new key and repeat the process in item 3 to add another new key below "Compact" and call this key "Command".

5.) Click on the new "Default" entry in the "Command" key and the key value dialog will be displayed. Enter the path to the version of Access used for this compact method on the computer, appending a "%1" at the end, and the adding the command line switch "/compact". Example: "C:\program files\Microsoft Office\msaccess.exe" "%1" /compact".



Back to the Tips List

How to Automatically compact your Access Db on a schedule
You can use Windows Scripting Host (WSH) and the Windows Task scheduler (Task scheduler is an option installed with IE 4 & 5 and is included in Windows 98 and 2000) or the AT Scheduler in NT, to easily create scripts that will perform different tasks on your db, such as nightly compacting. 

The sample script performs routine maintenance on application that has a temporary data db and a standard data db.  In this script, all data from the tables in a temporary db is deleted and then the db is compacted, then the script moves on and  compacts the backend db of the application.   This type of script can be run nightly on a file server.   You can write a similar script to instantiate an instance of Access itself, open a database and run a report.

Note that this script instantiates the DAO engine 3.51, used in Access 97 to do the compacting.  This reference should be changed if you are using Access 2000 to DAO 3.6.  In addition, this script is designed to be run by the WSH console executable cscript.exe, not by the Windows version of WSH (wscript), since it echoes various information to the dos box window it would run in.  

    'CompactDbs.vbs
    'Windows Scripting Host Script, for Win 95/98/NT/2000
    'Compatible with WSH Version 1 and 2
    'Written By ACG Soft, Ann Arbor, MI
    'Run from the command line using the cscript runtime engine
    'Do not run from the Windows interface unless all the 
    'Echos are deleted
    '-----------------------------------------------------

    Dim objEngine
    Dim objDB
    Dim strDb1, strDb1a
    Dim FSO
 
    WScript.Echo "**************************"
    Wscript.echo "Compacting Db Applications"
    WScript.Echo "-DO NOT CLOSE THIS WINDOW-"
    WScript.Echo "-It will close when done-"
    WScript.Echo "**************************"
    WScript.Echo ""

    strDb1 = "c:\TargetApp\TempData.mdb"   
    strDb1a = "c:\TargetApp\BackEndData.mdb"

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set objEngine = WScript.CreateObject("DAO.DBEngine.35")

    call CompactTemp(FSO, objEngine, strDb1, "Temp Data")
    call CompactDB(FSO, objEngine, strDB1a, "Live Backend Data")

    WScript.Echo ""
    WScript.Echo "*******************"
    WScript.Echo "All Jobs completed"
    WScript.Echo "*******************"
   
Function CompactTemp(objFSO, objEngine, strDb1, strDbName)
   'Deletes temp data and compacts the db.
   On Error Resume Next
   strdbtemp = Left(strDb1, len(strDb1)-3) & "tmp"
   Wscript.echo "Now Compacting " & strDbName
   If FSO.FileExists(strDb1) = False then 
      WScript.Echo "The database file " & strDb1 & " could not be found."
      Exit Function
   End if

   Set objDb = objEngine.Opendatabase(strDB1)
   With objDb
      For Each dbTableDef In .TableDefs
         strTblName = dbTableDef.Name
         If InStr(strTblName, "tmp") <> 0 Then 
             .Execute ("Delete * FROM " & strTblName)
         End If
      Next
   End With
   objDb.close
   'compact temp
   objEngine.CompactDatabase strDb1, strdbtemp
   If Err <> 0 Then Exit Function
   fso.deletefile strdb1
   fso.copyfile strdbtemp, strdb1
   fso.deletefile strdbtemp

End Function
    

Function CompactDB (objFSO, objEngine, strDb1a, strDbName)
   'compact db
   WScript.Echo "Now Compacting " & strDbName
   if FSO.FileExists(strDb1a) = False then 
      WScript.Echo "The database file " & strDb1a & " could not be found."
      Exit Function
   End If

   strdbtemp = Left(strDb1a, len(strDb1a)-3) & "tmp"
   objEngine.CompactDatabase strDb1a, strdbtemp
   If Err <> 0 Then Exit Function
   fso.deletefile strdb1a
   fso.copyfile strdbtemp, strdb1a
   fso.deletefile strdbtemp
End Function

 



Back to the Tips List

How to Add the option to your Right Click context menu to Open an Access file with any version of Access on your computer.
By default when you install any version of Access on a computer which has a previous version of Access also installed that you want to maintain, all databases with the extension of ".mdb" will be associated with the newer version of Access when you "right click" on the file in Windows Explorer.  You can add an option to the context menu to open the mdb in any version of Access so that it is easily opened in the version was created in.

To accomplish this do the following:

  1. Make sure you have a back up copy of your registry, in case of an error.
  2. Open the Registry Editor, and under the key "Hkey_Classes_Root", where X = the highest version of Access you have installed
  3. Under this Access.Application.X key click on the "Shell" key and then select "Edit" from the menu, "New", then "Key". After the key is added, name this key "Open with Access X." Where X is the version you want to add.
  4. Highlight the new key and repeat the process in item 3 to add another new key below "Open with Access X" and name this key "Command".
  5. Click on the new "Default" entry in the "Command" key and key value dialog will be displayed. Enter the path to the version of Access 2 on the computer, appending a "%1" at the end. Example: "C:\Access\msaccess.exe %1"

You should now have the option to open any access database on your system with any version of Access when right clicking in Windows Explorer.

Back to the Tips List