vba 在Access表单中,重新查询后如何返回上一条记录

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

In an Access form, how to return to previous record after requery

ms-accessvba

提问by PowerUser

This should be an easy one. This form is filtered by [Dismissed] = "N". When the user clicks the "Dismiss" button, the [Dismissed] field changes to "Y". After the requery, the form should then return to the same row where the user was previously at.

这应该是一件容易的事。此表单由 [Dismissed] = "N" 过滤。当用户单击“Dismiss”按钮时,[Dismissed] 字段变为“Y”。重新查询后,表单应返回到用户之前所在的同一行。

Private Sub DismissButton_Click()
   Me!Dismissed = "Y"
   MsgBox "Dismissed!", vbOKOnly
   Dim GoBackToThisRecord As Integer
   GobacktothisRecord = Me.CurrentRecord
   Me.Requery
   Set Me.CurrentRecord=GoBackToThisRecord
End Sub

However, even though the built-in help files say that CurrentRecord is a read/write property, I get an "Invalid use of property" error message on this last line.

然而,即使内置帮助文件说 CurrentRecord 是一个读/写属性,我在最后一行收到一条“属性的无效使用”错误消息。

After setting the [Dismiss]="Y", and requerying the form, how do I get the user back to his/her previous location in the form?

设置 [Dismiss]="Y" 并重新查询表单后,如何让用户回到他/她在表单中的先前位置?

采纳答案by David-W-Fenton

I don't understand how your solution can work if the form is filtered to a value that the edited record no longer matches -- if you're filtered on [Dismissed] = "N" then changing the current record's Dismissed field to Y should cause the requeried form to exclude the record you've just updated.

如果表单被过滤为编辑的记录不再匹配的值,我不明白您的解决方案如何工作 - 如果您在 [Dismissed] = "N" 上进行过滤,那么将当前记录的 Dismissed 字段更改为 Y 应该导致重新查询的表单排除您刚刚更新的记录。

That aside, I would never do it the way you've done it, as Me.CurrentRecord returns a number representing the position in the record. Since a requery can cause the number of records to change (e.g., somebody else edits or adds or deletes a record causing it to be included/excluded from the form's recordset) and the position of the sought-for record to change, I would use the PK instead.

除此之外,我永远不会像你那样做,因为 Me.CurrentRecord 返回一个代表记录中位置的数字。由于重新查询会导致记录数发生变化(例如,其他人编辑或添加或删除记录导致其被包含/排除在表单的记录集中)以及所寻求记录的位置发生变化,我会使用PK代替。

  Dim lngPK as Long

  lngPK = Me!MyPKID
  Me.Requery
  With Me.RecordsetClone
    .FindFirst "[MyPKID]=" & lngPK
    If Not .NoMatch Then
       If Me.Dirty Then
          Me.Dirty = False
       End If
       Me.Bookmark = .Bookmark
    End If
  End With

That won't deal with the filter issue, but I leave that aside, since it didn't seem to be the issue that I thought it would be from the description of the original problem.

那不会处理过滤器问题,但我把它放在一边,因为它似乎不是我认为从原始问题的描述中出现的问题。

回答by kiks73

The right way to move to the previous record, whether it is a new one or not, is

移动到上一个记录的正确方法,无论它是否是新记录,是

Me.Recordset.Move GoBackToThisRecord -1

回答by Falo

I use this function:

我使用这个功能:

Public Sub RequeryFormAndKeepCurrentlySelectedRecord(f As Form)
Dim Position As Long
  Position = f.CurrentRecord
  f.Requery
  If Position > 1 Then
    f.Recordset.move Position - 1
  End If
End Sub

回答by PowerUser

Nevermind. Fixed it myself. The last line is now:

没关系。自己修好了。最后一行是现在:

Me.Recordset.Move GoBackToThisRecord