How can I change the view of an MS Access subform at runtime in VBA code?
Asked Answered
G

3

5

This seems like it would be a simple affair, and I am sure I have done this before, but it has been a while since I have done any UI programming in Access. What I need to do is put a button on a form to toggle between datasheet and form view for a subform.

I have found a defaultview property, but nothing that looks like it would toggle the view of the form after it is already open.

Essentially I need the property I can fill in the following code..

sfEmployeeBatchEntry.Form.??? = acFormDS
Gastrectomy answered 25/3, 2009 at 15:9 Comment(2)
How to make it go to a Continuous Form viewRhenium
I suggest you ask a separate question on the site instead of asking questions in a comment.Gastrectomy
G
11

I found it on my own. I was missing it because it used the silly and clunky RunCommand syntax instead of a simple property or method on the control or form classes.

It ain't pretty, but for posterity, here is the answer.

'You have to set focus to the subform control or the change view call will'
'fail (UGH!)'
MyForm.mySubFormControl.SetFocus

'Change to datasheet view...'
DoCmd.RunCommand acCmdSubformDatasheet

'Change to Form View...'
DoCmd.RunCommand acCmdSubformFormView
Gastrectomy answered 25/3, 2009 at 15:37 Comment(2)
How to make it go to a Continuous Form viewRhenium
In my testing, the Continuous Form is only displayed with DoCmd.RunCommand acCmdSubformFormView if the Default View property is set to "Continuous Form" while the form is in design view. If the Default View is set to anything else, then it will only toggle between Datasheet and Single Form view. I did not do further testing/research whether the DefaultView can be changed during runtimeArtemis
M
0

I tried a number of different solutions I found on different sites. Some seemed unnecessarily complicated. I cleaned out some of the clutter and found this fits my needs the best.

 Dim intView As Integer
 intView = Me.Form.CurrentView

 If intView = 1 Then

    DoCmd.RunCommand (acCmdSubformDatasheetView)
    Else


    DoCmd.RunCommand (acCmdSubformFormView)
    End If

Exit Sub
Miki answered 20/7, 2017 at 19:11 Comment(0)
A
0

To Toggle the View of a subForm between Continuous and Datasheet, use this 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
Avellaneda answered 5/7, 2018 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.