4

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

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
4

2 回答 2

1

我相信我有一个答案,尽管它对我来说似乎很笨拙。此过程找到目标行,向下滚动直到目标行不可见,向上滚动回目标行,超出目标行(偏移)额外行,然后向下滚动回目标行。结果是目标行显示为第 (Offset + 1) 个可见行。如果行数 < 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
于 2012-10-26T01:39:42.120 回答
1

我不完全确定这是否就是您所需要的:
a)直接转到第 5 条记录,使用: DoCmd.GoToRecord acDataForm, Me.Name, acGoTo, 5
(Me.Name only if the code is in the form's own code module)
b)您可以查询当前位置奇怪的 Me.Recordset.AbsolutePosition
是,.AbsolutePosition 从 0 开始计数,goto 命令从 1 开始计数。

于 2012-10-25T20:23:23.093 回答