Supersize MsAccess: Microsoft Access and the Unexpected Shared Image Cache

Microsoft AccessMicrosoft Access (MSACCESS) is well known for it’s All-American ability to become obese without a moments hesitation. I had a accdb file that was around six times larger than I was expecting it to be.

The usual solution is a compact and repair.

In the old days before Access 2007, you would also look for embedded images and change them to runtime linked images because Access would store all images as uncompressed Device Independant Bitmaps.

Next would be the decompile, a secret and arcane ritual involving either a command window or the accursedly tiny Target box in a shortcut properties window.

Last would be the create-a-new-database-and-import-all-the-old-objects ploy and let Access start again.

However, my friend discovered another step you may want to check before that last dramatic step. Open a form in Design View, switch to the Format tab and see if you have any images in the Background Image dropdown. This is a history of images embedded in your database.

Supersize MsAccess (1)

But it’s not just a history. It’s also a copy of every image that has been embedded in your database. Ever.

Now if you’re thinking that you never embed images in your database, well, Access has an unpredictable habit of occasionally saving your runtime linked images when you save a module and you have a form open with runtime linked images. And these become embedded until you notice and set the Picture property back to none. And no matter how fast you noticed, Access may now have a copy of that image.

Also, this contains every image you selected from a Browse button on a picture control or that Background Image selector.

The reason they are there is that Access introduced a new PictureType: Shared. This is the default when you use the Insert Image button instead of the Image control button. When you use the Image control button, the PictureType is set to Embedded.

This is entirely reasonable and a nice feature. It means that you can design an interface using shared images and easily update the design by updating the single shared image. Also, if you are using the image on more than one form, the image is only stored once.

So where are they?

  • Go to Options, Current Database, Navigation Options and tick Show Hidden Objects and Show System Objects.
  • Open the table MSysResources and you will see all the images listed with a type of img. The attachment is the original full size image file.

Supersize MsAccess (2)

Removal

Access itself never deletes these images.

  • After making a backup of my database, I deleted all the records with Type img. (There are other items in here as well, so be careful not to delete them.) After that, my obese 157MB database became a svelte 14MB.
  • If you’ve only got a couple, you can go to Form Design, drop down Insert Image and right-click an existing image to delete or rename it. If it was being used as a Shared PictureType, those Image controls will no longer contain an image.

If you have a Microsoft Access 2007 onward database that is much larger than you would expect it to be and none of the usual recommendations do anything to shrink it, this may be the cause.

Microsoft Access VBA: Convert Query or SQL to String

I wanted to take a query and format part of its output into English; a list of names separated by commas.

The DAO Object Library includes a completely pointless Recordset.GetRows command which returns an unwieldy multi-dimensional array. How pointless and unwieldy? Well, the supplied example to Debug.Print FirstName, LastName and Title from Northwind.mdb’s Employees table is 90 lines long. Without GetRows it would take way less than 10 lines. Amazingly, when Microsoft introduced ADO, it would retain the spectacular uselessness of this command by continuing to return a multi-dimensional array.

These functions take a query or table name or SQL command and a simple string to format the output which can contain any text with fields from the query surrounded by square brackets.

The default parameters produce a comma-delimited list with the final item delimited with an “, and” but these can be replaced with anything.

Parameters governing what is returned when there is no data in the query and what is displayed on screen as the function executes are also included and are documented in the source code.

Examples

?FromQuery(“MyTable”,”[Name] ([Age]-years-old)”)

Peter (71-years-old), Paul (71-years-old), and Mary (72-years-old)

?FromQuery(“MyTable”,”[Name] ([Age]-years-old)”, vbCrLf, vbCrLf)

Peter (71-years-old)
Paul (71-years-old)
Mary (72-years-old)

Source Code

''' <summary>
''' Converts a query to a string. Each record is formatted using Fields where field names
''' are surrounded by square brackets. Each record is separated by Delimiter.
''' </summary>
'''
''' <paramref name="Fields">A string used to format each record. Field names surrounded by square
'''                         brackets are replaced with the field value. For example, "Name: [Name]"
'''                         would produce a record that looks something like "Name: Paul J. Champion"
'''
''' <paramref name="Delimiter">Each record is separated with this string.
'''                            For example, if the Delimiter is ", " you would get
'''                            "Record 1, Record 2, Record 3, " etc
'''
''' <paramref name="FinalDelimiter">The last record is separated with this string.
'''                                 For example,if the FinalDelimiter is ", and " you would get
'''                                 "Record 1, Record 2, and Record 3"
'''
''' <paramref name="NoRecords">If there are no records available, this message is returned unaltered.
'''
''' <paramref name="ProgressText">If ProgressText is supplied, then a progress bar is shown in status bar.
'''                               If ProgressText is set to an empty string, no progress bar is shown.
''' </paramref>
Public Function FromQuery( _
                QueryName As String, _
                Fields As String, _
                Optional Delimiter As String = ", ", _
                Optional FinalDelimiter As String = ", and ", _
                Optional NoRecords As String = "No data", _
                Optional ProgressText As String = "Processing FromSQL query") _
                As String
  FromQuery = FromSQL(CurrentDb.QueryDefs(QueryName).SQL, Fields, Delimiter, FinalDelimiter, NoRecords, ProgressText)
  End Function

''' <summary>
''' Converts a SQL query to a string. Each record is formatted using Fields where field names
''' are surrounded by square brackets. Each record is separated by Delimiter.
''' </summary>
'''
''' <paramref name="Fields">A string used to format each record. Field names surrounded by square
'''                         brackets are replaced with the field value. For example, "Name: [Name]"
'''                         would produce a record that looks something like "Name: Paul J. Champion"
'''
''' <paramref name="Delimiter">Each record is separated with this string.
'''                            For example, if the Delimiter is ", " you would get
'''                            "Record 1, Record 2, Record 3, " etc
'''
''' <paramref name="FinalDelimiter">The last record is separated with this string.
'''                                 For example,if the FinalDelimiter is ", and " you would get
'''                                 "Record 1, Record 2, and Record 3"
'''
''' <paramref name="NoRecords">If there are no records available, this message is returned unaltered.
'''
''' <paramref name="ProgressText">If ProgressText is supplied, then a progress bar is shown in status bar.
'''                               If ProgressText is set to an empty string, no progress bar is shown.
''' </paramref>
Public Function FromSQL( _
                SQL As String, _
                Fields As String, _
                Optional Delimiter As String = ", ", _
                Optional FinalDelimiter As String = ", and ", _
                Optional NoRecords As String = "No data", _
                Optional ProgressText As String = "Processing FromSQL query") _
                As String

  Dim Record As String

  Dim rs As DAO.Recordset ' requires Reference to DAO Object Library
  Set rs = CurrentDb.OpenRecordset(SQL)

  If Not rs.EOF Then

    ' Setup progress bar if required
    ' Initialise boolean variable as it will be quicker for later on
    Dim ShowProgress As Boolean
    ShowProgress = ProgressText <> ""

    If ShowProgress Then
      SysCmd acSysCmdInitMeter, ProgressText, 100
      rs.MoveLast ' to ensure recordset is populated
      rs.MoveFirst
    End If

    Dim FirstField As Boolean: FirstField = True

    Do While Not rs.EOF

      ' Reset Record
      Record = Fields

      ' Insert fields from SQL into RecordHTML
      Dim fld As DAO.Field
      For Each fld In rs.Fields
        Record = Replace$(Record, "[" & fld.Name & "]", Nz(fld.value), , , vbTextCompare)
      Next

      ' Add this record to return value FromSQL
      If Not FirstField Then
        If rs.AbsolutePosition + 1 = rs.RecordCount Then
          FromSQL = FromSQL & FinalDelimiter
        Else
          FromSQL = FromSQL & Delimiter
        End If
      End If
      FromSQL = FromSQL & Record

      ' Update progress bar, then move to next record
      If ShowProgress Then
        SysCmd acSysCmdUpdateMeter, rs.PercentPosition
      End If
      rs.MoveNext
      FirstField = False

    Loop ' While Not rs.EOF

    ' Clear progress bar if necessary
    If ShowProgress Then
      SysCmd acSysCmdClearStatus
    End If

  Else
    FromSQL = NoRecords
  End If

FromSQL_exit:
  On Error Resume Next
  rs.Close

  End Function

Ordinal Suffixes for all dates and numbers; Show 3rd instead of 3

Microsoft Access logoA surprising omission from the extensive number formatting goodies in Visual Basic (VB) is the ability to append the appropriate ordinal suffix to a number. This is most often used when writing dates in prose. For example, you would write 3rd February 2009, not 3 February 2009. This VB / VBA function returns your number with the appropriate suffix attached.

A number that ends in a 1 uses the suffix “st”, a 2 “nd”, a 3 “rd” and everything else “th”. Naturally, this being English, there are one or four exceptions that break the grammatical rule. Numbers ending in 11, 12 and 13 all have the suffix “th” and 0 has no suffix. The function below works correctly for any number I threw at it and you can also supply a string containing a number. This is useful for rendering dates which you could convert to a string using Format$ and then send to this function.

Examples

  • FormatOrdinal(1) produces “1st”
  • FormatOrdinal(2) produces “2nd”
  • FormatOrdinal(3) produces “3rd”
  • FormatOrdinal(4) produces “4th”
  • FormatOrdinal(11) produces “11th”
  • FormatOrdinal(111) produces “111th”
  • FormatOrdinal("1111") produces “1111th”
  • FormatOrdinal(1111.87) produces “1111.87”
  • FormatOrdinal("garbage") produces “garbage”
  • FormatOrdinal(Format$(#13 Feb 2009#, "d")) produces “13th”
  • FormatOrdinal(324989234842) produces “324989234842nd”

Code

”’

”’ Adds the ability to include the ordinal suffix for numbers, ie.,st, nd, rd, th
”’

Public Function FormatOrdinal( _
Number) _
As String

‘ Set a default return value
FormatOrdinal = (Number)

‘ Only add suffix if a whole numeric value was supplied
If IsNumeric(Number) Then

‘ Make sure the variant Number is now of a numeric data-type so we can perform
‘ numerical comparisons
Number = Val(Number)

If (Number = Int(Number)) And (Number <> 0) Then
‘ Use the last two digits of the number (between 0 and 99) for determining
‘ the suffix. We only use the whole part of the number (Int) and we we use
‘ the Abs function to make sure it is in the range 0 to 99. It is converted
‘ to a string (Format), the right two characters pulled off (Right$) and
‘ converted back to a number (Val).
Dim Remainder As Long
Remainder = Val(Right$(Format$(Int(Abs(Number))), 2))

‘ 2 character suffixes for numbers ending in 1 to 9 respectively
Const Suffixes = “st” & _
“nd” & _
“rd” & _
“th” & _
“th” & _
“th” & _
“th” & _
“th” & _
“th”

‘ Suffix is “th” if remainder is between 10 and 19 or if it is exactly
‘ divisible by 10
If ((Remainder >= 10) And (Remainder <= 19)) _ Or ((Remainder Mod 10) = 0) Then FormatOrdinal = Format$(Number) & "th" Else ' Pull suffix from constant Suffixes using the last digit doubled ((Remainder Mod 10) * 2) ' as a starting point FormatOrdinal = Format$(Number) & Mid$(Suffixes, ((Remainder Mod 10) * 2) - 1, 2) End If End If ' Number = Int(Number) End If ' IsNumeric(Number) End Function[/sourcecode] This code was written in Visual Basic for Applications (VB, VBA) for Microsoft Access 2002 (XP) on Windows Vista. It is based on code originally published by Chip Pearson and found via Google and experts-exchange.com. However, this code works for all numbers (not just dates, ie., 1-31) and is much more robust.

Microsoft Access Visual Basic Form Helper: SetOpacity

This function allows you to set the opacity for a PopUp form. Normally, forms are fully opaque and you cannot see through the form. When it is partially opaque, you can partially see what is behind the form.

I use it for smoothly fading in and out forms in my Microsoft Access application (specifically, a full-screen picture viewer).

To use:

  1. Save this code into a new module called FormHelper.
  2. Use FormHelper.SetOpacity as follows:
    • Call FormHelper.SetOpacity Form_YourForm, 0 to make your form fully transparent.
    • Call FormHelper.SetOpacity Form_YourForm, 100 to make your form fully opaque.
    • Call FormHelper.SetOpacity Form_YourForm, Opacity where Opacity is any value between 1 and 99 to make your form partially opaque (or partially transparent!).

The code is fully commented and contains more details on implementation and what the functions can and can’t do.

Option Compare Database
Option Explicit

Private Const Namespace$ = "FormHelper"

'''
''' Used by SetOpacity and SetTransparentColor
'''
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_COLORKEY = 1
Private Const LWA_ALPHA = 2
Private Const WS_EX_LAYERED = &H80000
Private Declare Function apiGetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
                         ByVal hwnd As Long, _
                         ByVal nIndex As Long) As Long
Private Declare Function apiSetLayeredWindowAttributes Lib "user32" Alias "SetLayeredWindowAttributes" ( _
                         ByVal hwnd As Long, _
                         ByVal color As Long, _
                         ByVal AlphaPercent As Byte, _
                         ByVal alpha As Long) As Boolean
Private Declare Function apiSetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
                         ByVal hwnd As Long, _
                         ByVal nIndex As Long, _
                         ByVal dwNewLong As Long) As Long

'''
''' Set opacity of form
'''
'''
''' Setting OpacityPercent to zero makes the form fully transparent.
'''       Setting OpacityPercent to 100 makes the form fully opaque.
'''
''' This only has an affect on forms whose PopUp property is True.
'''
Public Sub SetOpacity( _
           frm As Access.Form, _
           OpacityPercent As Byte)

  ' Perform checks on arguments
  ' Ensure frm is a PopUp form. Raise an error if it is not.
  If Not frm.PopUp Then
    Err.Raise 5, , "Invalid argument." & vbCrLf & Namespace$ & " cannot SetOpacity on form " & frm.Name & ". PopUp form required."
    Exit Sub
  End If

  ' Ensure OpacityPercent is between 0 and 100.
  ' Do not raise an error if out of range, simply fix it.
  If OpacityPercent < 0 Then
    OpacityPercent = 0
  ElseIf OpacityPercent > 100 Then
    OpacityPercent = 100
  End If
  ' --------------------------------------------------
  ' If we reach here, all arguments have been accepted
  ' --------------------------------------------------

  ' Convert supplied percentage value to one ranging between 0 and 255 for apiSetLayeredWindowAttributes
  Dim iAlpha As Integer
  iAlpha = (OpacityPercent / 100) * 255

  ' Get forms current extended attributes
  Dim attrib As Long
  attrib = apiGetWindowLong(frm.hwnd, GWL_EXSTYLE)
  ' Set form to have extended layered attribute
  apiSetWindowLong frm.hwnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED
  ' Set opacity
  apiSetLayeredWindowAttributes frm.hwnd, RGB(0, 0, 0), iAlpha, LWA_ALPHA

  End Sub

You can also download FormHelper.bas. This is always the latest complete version of this module and contains more functions than detailed here. The functions are fully commented.

This code was written in Visual Basic for Applications (VB, VBA) for Microsoft Access 2002 (XP) on Windows XP.

Microsoft Access Visual Basic Form Helper: Replacement for DoCmd.OpenForm for neater code

Frequently in Microsoft Access Visual Basic you will want to programmatically display a different record on a currently open form but this common requirement turns out to be a bit more fiddly than you might expect. Manually setting the Filter and FilterOn form properties or using RecordSetClone and FindFirst are all somewhat less than intuitive.

Microsoft provides optional arguments for their DoCmd.OpenForm function that allow you to specify which record or records to display when first opening the form. It is a very neat and a very useful shortcut. However, because it always runs through all your form initialisation code and appears to do something else mysterious, it can prove to be a slow method to use to reset a currently open form to display a different record or set of records and can move focus to an unexpected control (especially when your form has tabs and subforms).

My FormHelper.OpenForm avoids the usability performance hit of DoCmd.OpenForm by, if your form is already open, bringing your form to the top and updating the properties where possible. If the form is not open or you require properties that cannot be reset while the form is open, then the normal DoCmd.OpenForm is used.

To use:

  1. Save this code into a new module called FormHelper.
  2. Replace calls to DoCmd.OpenForm with FormHelper.OpenForm.

The code is fully commented and contains more details on implementation and what the functions can and can’t do.

''' <summary>
''' Opens form or, if open already, brings form to the top and activates it. Is an exact replacement for DoCmd.OpenForm.
''' </summary>
''' <remarks>
''' <para>There are some significant limitations on passing arguments to forms that are already open.
'''       <b>View</b>, <b>FilterName</b>, <b>WindowMode</b> and <b>OpenArgs</b> will all be ignored.
'''       If these arguments are supplied, the form will be closed and re-opened using DoCmd.OpenForm.
''' </para>
''' </remarks>
Public Function OpenForm( _
                FormName As String, _
                Optional View As AcFormView = acNormal, _
                Optional FilterName As String, _
                Optional WhereCondition As String, _
                Optional DataMode As AcFormOpenDataMode = acFormPropertySettings, _
                Optional WindowMode As AcWindowMode = acWindowNormal, _
                Optional OpenArgs As String)
  
  ' Determine whether we must use DoCmd.OpenForm
  Dim DoCmdOpenFormRequired As Boolean
  DoCmdOpenFormRequired = (View <> acNormal) Or (FilterName <> "") Or (WindowMode <> acWindowNormal) Or (OpenArgs <> "")
  
  If FormOpen(FormName) And Not DoCmdOpenFormRequired Then
  
    ' Bring open form to top
    BringToTop Forms(FormName)
    
    With Forms(FormName)
      ' Update Filter property using WhereCondition
      If WhereCondition <> .Filter Then
        .Filter = WhereCondition
        .FilterOn = (WhereCondition <> "")
      End If
      
      Select Case DataMode
        Case acFormAdd: .AllowAdditions = True: .AllowDeletions = False: .AllowEdits = False
        Case acFormEdit: .AllowAdditions = True: .AllowDeletions = True: .AllowEdits = True
        Case acFormReadOnly: .AllowAdditions = False: .AllowDeletions = False: .AllowEdits = False
      End Select
      
    End With
    
  Else
    ' Open form in standard way using DoCmd.OpenForm
    DoCmd.OpenForm FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs
  End If
  
  End Function

You can also download FormHelper.bas. This is always the latest complete version of this module and contains more functions than detailed here. The functions are fully commented.

This code was written in Visual Basic for Applications (VB, VBA) for Microsoft Access 2002 (XP) on Windows XP.

Microsoft Access Visual Basic Form Helper: BringToTop function

Microsoft Access Visual Basic Form Helper: BringToTop function

Microsoft AccessThis function allows you to bring a specific form to the top so that you can be certain that the user can see it.

To use:

  1. Save this code into a new module called FormHelper.
  2. Call FormHelper.BringToTop with the Form object you wish to bring to the top.

The code is fully commented and contains more details on implementation and what the functions can and can’t do.

Option Compare Database
Option Explicit

Private Const Namespace$ = "FormHelper"


''' 
''' Used by BringToTop
''' 
Private Declare Function apiBringWindowToTop Lib "user32" Alias "BringWindowToTop" ( _
                         ByVal hwnd As Long) _
                         As Long
                         
                         
                         
''' 
''' Brings form to top and activates it
''' 
''' 
''' Uses following Declare:
''' <code>Private Declare Function apiBringWindowToTop Lib "user32" Alias "BringWindowToTop" (ByVal hwnd As Long) As Long</code>
''' 
Public Sub BringToTop( _
           frm As Access.Form)
  apiBringWindowToTop frm.hwnd
  End Sub

You can also download FormHelper.bas. This is always the latest complete version of this module and contains more functions than detailed here. The functions are fully commented.

This code was written in Visual Basic for Applications (VB, VBA) for Microsoft Access 2002 (XP) on Windows XP.