vba 如何在 Access 窗体和数据表视图之间切换,并保持在同一记录上,而不进行过滤?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2062091/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:12:26  来源:igfitidea点击:

How do I switch between Access Form and Datasheet views, and remain on the same record, without filtering?

ms-accessformsvbaviews

提问by

In MS Access 2007, I want to switch between datasheet and form views, without filtering, and remain on the current record.

在 MS Access 2007 中,我想在数据表和表单视图之间切换,无需过滤,并保留在当前记录上。

Should I use a bookmark ? How ? or How might I place a button on the ribbon to switch views, without having to search for the record or use a filter.

我应该使用书签吗?如何 ?或如何在功能区上放置一个按钮来切换视图,而不必搜索记录或使用过滤器。

I need this to run Access 2007 Runtime, since it will be implemented on a non-licensed computer. Seems some of the ribbon butttons & groups are not showing even if defined:

我需要它来运行 Access 2007 Runtime,因为它将在未经许可的计算机上实现。即使定义了一些功能区按钮和组,似乎也没有显示:

I tried the "GroupViews" and "ViewsModeMenu" options in the ribbon but they do not work in the Runtime. Also, any ribbon options that change the view also requery to the first record in the dataset, instead of retaining the current record.

我尝试了功能区中的“GroupViews”和“ViewsModeMenu”选项,但它们在运行时不起作用。此外,更改视图的任何功能区选项也会重新查询数据集中的第一条记录,而不是保留当前记录。

回答by Albert D. Kallal

You could probably cobble together something that flips between the two views, but as an general rule, you far better off to build an continues form with a button that the user clicks on to launch a details form.

您可能会拼凑一些在两个视图之间翻转的东西,但作为一般规则,您最好构建一个带有按钮的继续表单,用户单击该按钮以启动详细信息表单。

Trying to control how and what the continues form displays as opposed to a form that allows the user to view/edit/print/verify and simply control the user interface in a reasoned manor suggests that you are FAR BETTER off to launch another form. And, not only does this solve a host of problems, but it only one or 2 lines of code, and you don't loose your spot in that continues form to allow the user to launch + view other forms. Eg:

试图控制继续表单显示的方式和内容,而不是允许用户查看/编辑/打印/验证并在合理的庄园中简单地控制用户界面的表单,这表明您最好启动另一个表单。而且,这不仅解决了许多问题,而且只解决了一两行代码,而且您不会在继续表单中失去自己的位置,以允许用户启动 + 查看其他表单。例如:

Docmd.OpenForm "frmDetails",,,"id = " & me!id

Here is screen shot, and what really nice about continues forms in access is buttons and objects repeat for you:

这是屏幕截图,在访问中继续表单的真正好处是按钮和对象为您重复:

alt textAnd, here another one:

替代文字而且,这里还有一个:

alt text
(source: shaw.ca)

替代文字
(来源:shaw.ca

Again, note how the button just repeats, and the above two forms uses the one line of code I posted above.

再次注意按钮是如何重复的,上面的两种形式使用了我上面发布的一行代码。

Continues forms are a great solution, and I don't think it worth the trouble or efforts to try and flip between the views. And the above ideas work well for runtime also.

继续表单是一个很好的解决方案,我认为尝试在视图之间切换不值得麻烦或努力。并且上述想法也适用于运行时。

回答by Fionnuala

Have you created a custom ribbon (http://www.databasedev.co.uk/access2007ribbon.html)?

您是否创建了自定义功能区 ( http://www.databasedev.co.uk/access2007ribbon.html)?

This sample RibbonXML creates two buttons, the first to close the form and the second to show a particular form in DatasheetView. You can add a reference to the Ribbon using the Other tab of a form's property sheet.

此示例 RibbonXML 创建了两个按钮,第一个用于关闭表单,第二个用于在 DatasheetView 中显示特定表单。您可以使用表单属性表的“其他”选项卡添加对功能区的引用。

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="tab1" label="Object Helpers">
                <group id="grp1" label="Helpers">
                    <!-- close current object button -->
                    <button id="btnCloseObject" label="Close Current Object"
                            size="large"
                            imageMso="PrintPreviewClose"
                            onAction="OnCloseCurrentObject"/>
                     <!-- show datasheet -->
                    <button id="btnShowDatasheet" label="Show Datasheet"
                            size="large"
                            imageMso="AccessFormDatasheet"
                            onAction="ShowDatasheet"/>
               </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

You will also need some code and a reference to the Microsoft Office 12.0 or 14.0 Object Library (Tools->References in the code window).

您还需要一些代码和对 Microsoft Office 12.0 或 14.0 对象库的引用(代码窗口中的工具->引用)。

Public Sub OnCloseCurrentObject(ctl As IRibbonControl)
    DoCmd.Close CurrentObjectType, CurrentObjectName
End Sub

Public Sub ShowDatasheet(ctl As IRibbonControl)
    ''This saves the current unique ID
    CurRec = Forms!Test!TransactionID
    DoCmd.RunCommand acCmdDatasheetView

    ''This finds the saved ID, however, it is not necessary in Access 2010
    ''Because the bookmark is kept from form view
    Forms!Test.Recordset.FindFirst "TransactionID=" & CurRec
End Sub

More information: http://www.accessribbon.de/en/index.php?Downloads
http://blogs.msdn.com/access/archive/2007/09/24/ribbon-customization-closing-the-currently-open-object.aspx

更多信息:http: //www.accessribbon.de/en/index.php ?
Downloads http://blogs.msdn.com/access/archive/2007/09/24/ribbon-customization-closure-the-currently-打开对象.aspx