简体   繁体   English

如何在运行时使用VBA代码更改MS Access子窗体的视图?

[英]How can I change the view of an MS Access subform at runtime in VBA code?

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. 这似乎是一件简单的事情,并且我确定我之前已经做过,但是自从我在Access中完成任何UI编程以来已经有一段时间了。 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. 我已经找到了defaultview属性,但是看起来像没有什么会在窗体打开后切换窗体的视图。

Essentially I need the property I can fill in the following code.. 本质上,我需要可以在以下代码中填写的属性。

sfEmployeeBatchEntry.Form.??? = acFormDS

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. 我之所以错过了它,是因为它使用了笨拙的RunCommand语法,而不是控件或表单类上的简单属性或方法。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM