Change Default View property of subform via VBA (1 Viewer)

dpelizzari

Registered User.
Local time
Yesterday, 23:42
Joined
Jun 10, 2010
Messages
26
I have a form with a subform where I want to select (via button) which format to open the form in. Opening the form is simple, DoCmd.OpenForm (FormName),acFormDS to open in datasheet, however, the subform opens in default view (single form). Can you programmatically set the property on the subform to change the Default View from Single Form to Datasheet and back?
 

Simon_MT

Registered User.
Local time
Today, 04:42
Joined
Feb 26, 2007
Messages
2,177
Personally I would not bother with Datashet and convert the subform to a Continuous Form. If you want Single Form create another Form.

Simon
 

dpelizzari

Registered User.
Local time
Yesterday, 23:42
Joined
Jun 10, 2010
Messages
26
Thanks, Simon, I was hoping instead of me having to create a seperate form/subform I could programmatically change the Default View prior to opening the subform, adding one or two lines of code would be quicker and reduce maintenance, since both sets of form/subform would have identical information on them. If I needed to add a field, I could add it to one form instead of two.
 

dpelizzari

Registered User.
Local time
Yesterday, 23:42
Joined
Jun 10, 2010
Messages
26
Well, I found a solution online, so I thought I would share. As part of the on-click event for the button, I have added the following code before opening the form:
Code:
DoCmd.OpenForm "ISD_Lease_Subform", acDesign, , , , acHidden
Forms!ISD_Lease_Subform.DefaultView = 0
DoCmd.Close acForm , "ISD_Lease_Subform", acSaveYes
then open the form as it normally would.

This updates the subform ISD_Lease_subform to default view of Single Form. I also added the same code to the Datasheet button, with the exception of DefaultView = 2 for datasheet view.

This will allow me to have only one set of forms to maintain, but allow the users to go into single entry mode or "bulk" update mode.
 

dbDesigner

New member
Local time
Today, 05:42
Joined
Jul 5, 2018
Messages
1
To change the View of a SubForm from a Main Form, create a button called "cmdToggleView" with the following On Click Event:

(Substitute the SubForm Name with your own Subform Name)

Code:
Private Sub cmdToggleView_Click()

    If Me.frmViewDetailedTransactionsSub.Form.CurrentView = 1 Then
        Me.frmViewDetailedTransactionsSub.SetFocus
        DoCmd.RunCommand acCmdSubformDatasheetView
        Exit Sub
    End If
    
    If Me.frmViewDetailedTransactionsSub.Form.CurrentView = 2 Then
        Me.frmViewDetailedTransactionsSub.SetFocus
        DoCmd.RunCommand acCmdSubformFormView
        Exit Sub
    End If
    
End Sub
 

Users who are viewing this thread

Top Bottom