如何在运行时在 VBA 代码中更改 MS Access 子窗体的视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/682030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the view of an MS Access subform at runtime in VBA code?
提问by JohnFx
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.
这似乎是一件简单的事情,我确信我以前做过这件事,但是我已经有一段时间没有在 Access 中完成任何 UI 编程了。我需要做的是在表单上放置一个按钮,以便在子表单的数据表和表单视图之间切换。
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
回答by JohnFx
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
回答by taml
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
回答by dbDesigner
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