重新查询时如何避免进入第一条记录? [英] How to keep from going to first record when requerying?

查看:48
本文介绍了重新查询时如何避免进入第一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Access 2010 中制作一个表单.我正在尝试制作一个移动到下一条记录(或第一条记录,如果它在最后)的按钮,但因为我想考虑其他用户对数据集的更新在此期间发生的,我正在重新查询表单,然后再转到下一条记录.

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.

我正在使用以下代码,改编自 this SO post:

I'm using the following code, adapted from this SO post:

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

它工作正常,除了 .requery 导致表单在返回到当前记录之前短暂返回到第一条记录,然后返回到下一条记录.我不希望它这样做 - 有什么方法可以在 .requery 发生时保持当前记录显示在表单中,而不是在瞬间显示第一条记录.FindFirst 正在寻找 CurrentID 的记录?

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?

推荐答案

因此,正如 Remou 在 iDevlop 的答案下的评论中所写的那样,ODBC 刷新不显示新记录.我怀疑这是这里的问题,因为表单是从链接表中获取数据.

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.

我仍然需要有自己的上一个/下一个按钮,因为我希望在传递最后一条记录时能够循环回到第一条记录.但是我修改了代码;我不是每次都重新查询,而是检查该记录是否是最后一条记录,并且仅在我单击最后一条记录的 Next 或第一条记录的 Prev 时才重新查询.这样就很好的解决了问题.

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.

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

这篇关于重新查询时如何避免进入第一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆