vba 重新查询时如何避免进入第一条记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9235270/
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
How to keep from going to first record when requerying?
提问by sigil
Making a form in Access 2010. I'm trying to make a button that moves to the next record (or the first if it's at the end), but because I want to account for other users' updates to the data set that have occurred in the meantime, I'm requerying the form before going to the next record.
在 Access 2010 中制作一个表单。我正在尝试制作一个移动到下一条记录的按钮(如果它在最后,则是第一个),但是因为我想考虑其他用户对已发生的数据集的更新同时,我在转到下一条记录之前重新查询表单。
I'm using the following code, adapted from this SO post:
我正在使用以下代码,改编自这篇 SO 帖子:
Private Sub NextRec_Click()
Dim currentID As Long
currentID = Me.id.Value
'Here is where the requery brings the form back to the first record
Me.Requery
With Me.RecordsetClone
.FindFirst "id=" & currentID
If Not .NoMatch Then
If Me.Dirty Then
Me.Dirty = False
End If
Me.Bookmark = .Bookmark
End If
End With
If Me.CurrentRecord < Me.Recordset.RecordCount Then
DoCmd.GoToRecord , , acNext
Else
DoCmd.GoToRecord , , acFirst
End If
End Sub
It's working fine, except that .requery
causes the form to briefly return to the first record before going back to the current record and then on to the next record. I don't want it to do this--is there any way I can just keep the current record showing in the form while .requery
takes place, instead of showing the first record for the split second while .FindFirst
is looking for the record at CurrentID
?
它工作正常,除了.requery
导致表单在返回到当前记录然后返回到下一条记录之前短暂返回到第一条记录。我不希望它这样做 - 有什么办法可以在发生时保持当前记录显示在表单中.requery
,而不是在.FindFirst
寻找记录时显示第一条记录CurrentID
?
采纳答案by sigil
So, as Remou wrote in the comments under iDevlop's answer, ODBC refresh doesn't show new records. I suspect that is the issue here because the form is getting its data from a linked table.
因此,正如 Remou 在 iDevlop 答案下的评论中所写,ODBC 刷新不显示新记录。我怀疑这是这里的问题,因为表单是从链接表中获取数据的。
I'll still need to have my own Prev/Next buttons because I want to be able to loop back to the first record when I pass the last record. But I've modified the code; instead of requerying every time, I check to see if the record is the last one, and only requery when I click Next on the last record, or Prev on the first. This solves the problem adequately.
我仍然需要有自己的 Prev/Next 按钮,因为我希望在传递最后一条记录时能够循环回到第一条记录。但是我修改了代码;我不是每次都重新查询,而是检查该记录是否是最后一条,并且仅在我单击最后一条记录的 Next 或第一条记录的 Prev 时才重新查询。这样就可以很好的解决问题了。
Modified code for NextRec_Click
as follows:
修改后的代码NextRec_Click
如下:
Private Sub NextRec_Click()
Dim currentID As Long
currentID = Me.id.Value
If Me.CurrentRecord = Me.RecordsetClone.RecordCount Then
Me.Requery
With Me.RecordsetClone
.FindFirst "id=" & currentID
If Not .NoMatch Then
If Me.Dirty Then
Me.Dirty = False
End If
Me.Bookmark = .Bookmark
End If
End With
If Me.CurrentRecord < Me.Recordset.RecordCount Then
DoCmd.GoToRecord , , acNext
Else
DoCmd.GoToRecord , , acFirst
End If
Else
DoCmd.GoToRecord , , acNext
End If
End Sub
回答by mwolfe02
Requery the recordset, not the form itself:
重新查询记录集,而不是表单本身:
Me.Requery '<-- resets the current record to the first one in the recordset
Me.Recordset.Requery '<-- doesn't affect the current record
回答by Patrick Honorez
Sorry but I am strongly against those programmers who reinvent a NEXT button that is already provided.
If you need to do things when the user moves to the next record, use the OnCurrent event. This way you will also trap Page Down keys that users are entitled to use.
抱歉,我强烈反对那些重新发明已经提供的 NEXT 按钮的程序员。
如果您需要在用户移动到下一条记录时执行某些操作,请使用 OnCurrent 事件。通过这种方式,您还将捕获用户有权使用的 Page Down 键。
For me that code is unnecessary and a waste of resource. The recordset IS automatically updated every n seconds by Access (as specified in the refresh interval).
对我来说,这些代码是不必要的,而且是一种资源浪费。记录集 IS 每 n 秒由 Access 自动更新(在刷新间隔中指定)。
Is is true that deleted records stay there and appear as #deleted# and new records, might not appear unless you requery. But this is rarely a problem, and requery is really heavy and unfriendly.
确实删除的记录会留在那里并显示为 #deleted# 和新记录,除非您重新查询,否则可能不会出现。但这很少成为问题,requery 真的很繁重而且不友好。
Edit, after seeing @sigil's own reply:
Just an idea to reduce the number of recordset refreshes, which seem important to you: you might compare Me.Recordset.RecordCount
with dCount("*", Me.RecordSource)
and only requery when those differ.
编辑,看到@印记自己的答复后:
只是一个想法,以减少记录刷新,这似乎对你很重要的数字:你可能会比较Me.Recordset.RecordCount
有dCount("*", Me.RecordSource)
只有重新查询时有所不同。