[AccessD] how to change form default view via VBA

A.D.TEJPAL adtp at hotmail.com
Wed Apr 18 01:21:25 CDT 2007


Billy,

    Sample public subroutine named P_ToggleView(), in form's module, as given below, should enable you to toggle the view between datasheet & normal form as desired. 

    You can call this routine from double click event of various controls. Whenever the user dbl-clicks any of the controls (in form view) or any of the columns (in datasheet view), the existing view will change-over to the other one.

    If you wish to toggle the view of target form (named F_Test) by clicking a button on another form (named F_Test_02), the routine in module of form F_Test can be called via click event of command button on form F_Test_02. While doing so, care is to be taken to first select form F_Test as shown in the sample code below, for module of form F_Test_02.

    Note - When toggling views, it is often desirable to simultaneously take care of column widths (in datasheet view) and settings for scroll bars & dividing lines, as shown in the sample code below.

Best wishes,
A.D.Tejpal
---------------

Code module of form F_Test
(ID, SDate, Product & Qty are 
 names of controls on the form)
=================================
Public Sub P_ToggleView()
    ' Toggles between Form & Datasheet views
    
    ' Check current view and change accordingly
    ' Value of 2 stands for Datasheet view
    If Me.CurrentView = 2 Then
        ' Change to Form view
        DoCmd.RunCommand acCmdFormView
        
        ' Remove scroll bars & dividing lines
        Me.ScrollBars = 0      ' Neither
        Me.DividingLines = False
    Else
        ' Change to Datasheet view
        DoCmd.RunCommand acCmdDatasheetView
        
        ' Adjust column widths in datasheet view
        ID.ColumnWidth = 1500               ' (A)
        SDate.ColumnWidth = 2000          ' (B)
        Product.ColumnWidth = 3000       ' (C)
        Qty.ColumnWidth = 1550             ' (D)
        
        ' Restore scroll bars & dividing lines
        Me.ScrollBars = 3     ' Both
        Me.DividingLines = True
    End If
End Sub
=================================

Code module of form F_Test_02
=================================
Private Sub CmdToggleView_Click()
    DoCmd.SelectObject acForm, "F_Test", False
    Forms("F_Test").P_ToggleView
End Sub
=================================

  ----- Original Message ----- 
  From: Billy Pang 
  To: Access Developers discussion and problem solving 
  Sent: Wednesday, April 18, 2007 02:39
  Subject: [AccessD] how to change form default view via VBA


  Hello:

  Is it possible to change the default view of a form in MS Access using VBA?

  I would like to have the user toggle between the datasheet view and the form view using a button

  thanks in advance

  Billy



More information about the AccessD mailing list