Code Examples For RunCommand Constants

Best Fit For Column

acCmdColumnWidth

This routine allows you to set a column to be best fit. There are a number of steps required to ensure this example works.

  1. Add the code to a module
  2. Add a button to a toolbar
  3. Set ther action for the new toolbar item to
    =TBBestFit()

Also this code uses SendKeys which is known to sometimes cause problems.

'***************** Code Start *******************
SendKeys information

' This code was originally written by Terry Wickenden.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.

Function TBBestFit()

  On Error Goto ErrHandler

  SendKeys "%B", False
  DoCmd.RunCommand acCmdColumnWidth
  Exit Function

ErrHandler:

  Select Case Err
    Case 2046
      'Command not available
      MsgBox "Column width is not available at this time.", vbCritical, "Not Available"
    Case 2501
      'Cancel selected in dialog box - do nothing
    Case Else
      MsgBox Err.Number & ":-" & vbCrLf & Err.Description
  End Select

End Function
'****************** Code End ********************