Code Examples

Change Subform View

acCmdSubformDatasheet

This routine changes the view of a SubForm between Datasheet View and Form View. In order for this to work the SubForm must have its Views Allowed Property set to Both. The button to change the view must also be on the Main Form not the SubForm.

This example uses a button called cmdDatasheet and a subform control on the Main Form called subTestForm. It changes the caption of the button to reflect the change that will occur when clicked.

'***************** Code Start *******************
' 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.

Private Sub cmdDatasheet_Click()

  On Error GoTo ErrCmdDatasheet

  Me.subTestForm.SetFocus
  DoCmd.RunCommand acCmdSubformDatasheet
  If Me.cmdDatasheet.Caption = "Datasheet View" Then
    Me.cmdDatasheet.Caption = "Form View"
  Else
    Me.cmdDatasheet.Caption = "Datasheet View"
  End If
  Exit Sub

ErrCmdDatasheet:

  Select Case Err
    Case 2046
      'Datasheet view not available
      MsgBox "This command not available at this time"
      Exit Sub
    Case Else
      MsgBox Err.Number & vbCrLf & Err.Description
      Exit Sub
  End Select

End Sub

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

© 1998 - 2011 Terry Wickenden TKW Design Site developed maintained and hosted by TKW Design. This site is best viewed at 1024 x 768. Optimised for Firefox.