Access 2007 VBA:如何获取/更改当前记录在屏幕上的位置

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

Access 2007 VBA: how to get / change the current record's position on the screen

vbams-accessms-access-2007

提问by Eric

I have an Access 2007 form that displays 40 rows. When I open it, I would like the current record's row to be displayed at row position #5, out of the 40 rows displayed. In other words, there should be 4 rows above the current row, and 35 rows below it.

我有一个显示 40 行的 Access 2007 表单。当我打开它时,我希望当前记录的行显示在显示的 40 行中的第 5 行位置。换句话说,当前行上面应该有 4 行,它下面应该有 35 行。

MoveUpDownis my first cut at doing this, but currently it only works if the current record's row (as moved to by FindFirst) is initially displayed at row positions #1 through #5. How do I find what the current row's displayed position is, relative to the displayed rows, and how do I change it? Thanks.

MoveUpDown是我第一次这样做,但目前它只有在当前记录的行(由FindFirst移动到)最初显示在行位置 #1 到 #5时才有效。如何找到当前行相对于显示行的显示位置,以及如何更改它?谢谢。

Private Sub OpenMyFormTo(sqlWhere As String)
    Dim rs As DAO.Recordset, frm As Form
    DoCmd.Openform "myForm", acNormal
    Set frm = Forms("myForm").Form
    Set rs = frm.RecordsetClone
    If Not (rs.BOF And rs.EOF) Then
        rs.MoveFirst
        rs.FindFirst sqlWhere
        frm.Bookmark = rs.Bookmark
        MoveUpDown frm.Recordset, 5
    End If
End Sub

Private Sub MoveUpDown(rs As DAO.Recordset, RowsToMove As Integer)
    Dim RowsActuallyMoved As Integer, i As Integer
    RowsActuallyMoved = 0
    For i = 1 To RowsToMove
        If Not rs.BOF Then
            rs.MovePrevious
            RowsActuallyMoved = RowsActuallyMoved + 1
        End If
    Next i
    For i = 1 To RowsActuallyMoved
        If Not rs.EOF Then rs.MoveNext
    Next i
End Sub

回答by KekuSemau

I'm not totally sure if this is all you need:
a) to go directly to the the 5th record, use: DoCmd.GoToRecord acDataForm, Me.Name, acGoTo, 5
(Me.Name only if the code is in the form's own code module)
b) you can query the current position with Me.Recordset.AbsolutePosition
Oddly, .AbsolutePosition starts to count at 0, the goto-command at 1.

我不完全确定这是否是您所需要的全部:
a)直接转到第 5 条记录,使用:(DoCmd.GoToRecord acDataForm, Me.Name, acGoTo, 5
仅当代码在表单自己的代码模块中时才使用 Me.Name)
b)您可以查询当前位置与 Me.Recordset.AbsolutePosition
奇怪的是,.AbsolutePosition开始于在0计数时,转到指令。

回答by Eric

I believe I have an answer, although it seems clunky to me. This procedure finds the target row, scrolls down until the target row is not visible, scrolls back up to the target row, overshoots the target by (Offset) additional rows, then scrolls back down to the target row. The result is that the target row is displayed as the (Offset + 1)th visible row. If the number of rows < Offset, then it is displayed as the last row.

我相信我有一个答案,尽管它对我来说似乎很笨拙。此过程查找目标行,向下滚动直到目标行不可见,向上滚动回目标行,超出目标行(偏移),然后向下滚动回目标行。结果是目标行显示为第 (Offset + 1)th 可见行。如果行数 < Offset,则显示为最后一行。

Private Sub DisplayWithOffset(frm As Form, sqlWhere as String, Offset As Integer)

    Dim rs As DAO.Recordset, DetailsHt As Long, RowHt As Long, i As Integer
    Dim RowsVisible As Integer, BookmarkAP As Integer, RowsMoved As Integer

    DetailsHt = frm.InsideHeight _
              - frm.Section(acHeader).Height - frm.Section(acFooter).Height
    RowHt = frm.Section(acDetail).Height
    RowsVisible = IIf(RowHt > 0, DetailsHt Mod RowHt, 0)
    Set rs = frm.Recordset

    If Not (rs.BOF And rs.EOF) Then

        rs.MoveLast
        rs.FindFirst sqlWhere

        BookmarkAP = rs.AbsolutePosition

        RowsMoved = 0
        For i = 1 To RowsVisible
            If rs.AbsolutePosition + 1 < rs.RecordCount Then
                rs.MoveNext
                RowsMoved = RowsMoved + 1
            End If
        Next i

        Do Until rs.AbsolutePosition = BookmarkAP
            rs.MovePrevious
        Loop

        RowsMoved = 0
        For i = 1 To Offset
            If rs.AbsolutePosition > 0 Then
                rs.MovePrevious
                RowsMoved = RowsMoved + 1
            End If
        Next i

        For i = 1 To RowsMoved
            rs.MoveNext
        Next i

    End If

End Sub