gpt4 book ai didi

vba - Ms Access VBA 转到最后一条记录

转载 作者:行者123 更新时间:2023-12-03 20:17:13 26 4
gpt4 key购买 nike

我正在尝试打开一个表单并移至最后一条记录。
我正在使用以下

 DoCmd.RunCommand acCmdRecordsGoToLast

表单打开并转到最后一条记录,但它隐藏了其他记录(我必须使用滚动条)。这可能会使用户混淆,认为没有其他记录。

是否可以转到最后一条记录并查看最后 10 条记录?

最佳答案

这是你的幸运日——这出奇的不平凡,我前段时间为此编写了一个函数。

'---------------------------------------------------------------------------------------
' Procedure : FormGotoEnd
' Author : Andre
' Purpose : Go to the last record of a continuous form, but don't scroll that record to the top
' (as DoCmd.RunCommand acCmdRecordsGoToLast would do).
' Instead scroll up so that the last record is visible at the bottom of the form.
' Parameters: F = the form, can be a subform
' AddtlEmptyRowsBottom = if you want to have room for more than one empty row, for data entry forms
'
' Call this sub e.g. in Form_Load() or in Form_Current of the parent form, like this:
' Call FormGotoEnd(Me)
' or Call FormGotoEnd(Me!SubformControl.Form, 3)
'---------------------------------------------------------------------------------------
'
Public Sub FormGotoEnd(F As Form, Optional AddtlEmptyRowsBottom As Long = 0)

Dim DetailSectionHeight As Long
Dim nVisible As Long
Dim nRecords As Long

On Error GoTo FormGotoEnd_Error

' Calculate height of full details section: Window height minus header+footer
DetailSectionHeight = F.InsideHeight
' Ignore errors if form has no header or footer
On Error Resume Next
If F.Section(acHeader).Visible Then
DetailSectionHeight = DetailSectionHeight - F.Section(acHeader).Height
End If
If F.Section(acFooter).Visible Then
DetailSectionHeight = DetailSectionHeight - F.Section(acFooter).Height
End If
On Error GoTo FormGotoEnd_Error

' Number of visible records in details section
nVisible = CLng(DetailSectionHeight / F.Section(acDetail).Height)

' Nothing to do if the form has no records
If F.RecordsetClone.RecordCount > 0 Then
' For complex record source and/or many records, Access may not know .RecordCount yet
' -> calculate via .MoveLast
F.RecordsetClone.MoveLast
nRecords = F.RecordsetClone.RecordCount
' Nothing to do if all records are visible
If nRecords > nVisible Then

' Move to last record. Use .Bookmark so the subform doesn't need to get focus
F.Bookmark = F.RecordsetClone.Bookmark
' This is the important part!
' Add 2 to AddtlEmptyRowsBottom, in order to see the empty data-entry record plus one empty line
F.SelTop = nRecords - nVisible + 2 + AddtlEmptyRowsBottom
' Make sure the last record is selected
F.Bookmark = F.RecordsetClone.Bookmark

End If
End If

FormGotoEnd_Exit:
On Error GoTo 0
Exit Sub

FormGotoEnd_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in FormGotoEnd", vbExclamation
Resume FormGotoEnd_Exit

End Sub

关于vba - Ms Access VBA 转到最后一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49748681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com