MS访问任意版本。
我有一个连续的表单,它基于可以以多种方式排序的查询来显示记录集的一部分。假设表单显示记录集的n条记录(其中n>1)。表单中的第一行不需要显示记录集的第一条记录。选择其中一条记录(假设第x行的n条记录)。
在Form.Requery之后,记录集的第一条记录将始终位于窗体的第一行。第一条记录也会被选中。
这可不妙。我希望在Form.Requery之后恢复表单,以便所选记录仍然位于表单中的第x行,前提是这始终是可能的。显示在x:th行上方和下方的记录显然取决于记录集的底层查询。
在我的例子中,如果有一种方法可以计算在Form.Requery之前,表单中选定行和第一行之间有多少行,就可以实现这一点。
如果选择了表单的第一行,则该计算将(与显示的记录集的哪一部分无关)得到0,如果选择了表单的第二行,则得到1,依此类推。
我该怎么做?
还是有别的方法可以达到同样的效果?
发布于 2020-11-20 16:34:40
如果表单中有任何唯一的列,那么可以这样做:
Dim uniqueId : uniqueId = me.uniqueField_Value
' uniqueId hold the current selected unique field value
Form.Requery
With Me.RecordsetClone
' if uniqueId is numeric use next line
.FindFirst "[uniqueField] = " uniqueId
' if uniqueId is string uncomment next line and comment previous line
' .FindFirst "[uniqueField] = " & """" & uniqueId & """"
If Not .NoMatch Then
Me.Bookmark = .Bookmark
End If
End With发布于 2020-11-20 19:50:20
我使用这个函数--参见注释中的解释。
'---------------------------------------------------------------------
' Requery a continuous form, keeping the selected record and scroll position as it is before
' With ideas by Stephen Lebans - http://www.lebans.com/SelectRow.htm
'---------------------------------------------------------------------
Public Sub FormRequeryPosition(F As Form)
On Error GoTo Err_FormRequeryPosition
Dim OrigSelTop As Long
Dim RowsFromTop As Long
Dim OrigCurrentSectionTop As Long
' Must cache the current props because Requery will reset them
OrigSelTop = F.SelTop
OrigCurrentSectionTop = F.CurrentSectionTop
' Turn off screen redraw
F.Painting = False
' Requery the Form
F.Requery
' Calculate how many rows, if any, the selected row was from the top prior to the Requery
' Check if Header is visible or not
If F.Section(acHeader).Visible = True Then
RowsFromTop = (OrigCurrentSectionTop - F.Section(acHeader).Height) / F.Section(acDetail).Height
Else
RowsFromTop = OrigCurrentSectionTop / F.Section(acDetail).Height
End If
' Setting the SelTop property forces this row to appear at the top of the Form.
' We will subtract the number of rows required, if any, so that the original
' current row remains at the original position prior to the Requery.
' First set the current record to the last record.
' This is required due to the method that the Access GUI manages the ScrollBar.
' Prevent error on empty form
If F.RecordsetClone.RecordCount > 0 Then
' With complex record source or many records, Access may not know .RecordCount yet -> use MoveLast to get it
F.RecordsetClone.MoveLast
F.SelTop = F.RecordsetClone.RecordCount
F.SelTop = OrigSelTop - RowsFromTop
DoEvents
F.Painting = True
' Now setfocus back to the original row prior to the Requery
' In unknown conditions this raises a runtime error 3001 "Invalid argument"
On Error Resume Next
F.RecordsetClone.AbsolutePosition = F.CurrentRecord + RowsFromTop - 1
If Err.Number = 0 Then
F.Bookmark = F.RecordsetClone.Bookmark
End If
End If
Exit_FormRequeryPosition:
F.Painting = True
Exit Sub
Err_FormRequeryPosition:
MsgBox "Error " & Err.Number & ": " & Err.Description & vbCrLf & "Line: " & Erl
Resume Exit_FormRequeryPosition
End Subhttps://stackoverflow.com/questions/64925712
复制相似问题