vba 访问:移动到下一条记录,直到 EOF

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

Access: Move to next record until EOF

ms-accessvba

提问by Rick

I need to loop through a form by moving to the next record in the recordset.

我需要通过移动到记录集中的下一条记录来遍历表单。

I am using the Form_Current event to loop thru. I have used a couple of statements and have different outcomes.

我正在使用 Form_Current 事件进行循环。我使用了几个语句并且得到了不同的结果。

This one sometimes crashes and gives the error message: "You can't go to the specified record."

这个有时会崩溃并给出错误消息:“您无法转到指定的记录。”

DoCmd.GoToRecord , , acNext

This one only goes upto 72 records and stops.

这个最多只能有 72 条记录并停止。

DoCmd.RunCommand acCmdRecordsGoToNext

This one only goes upto 129 records and stops.

这个最多只能有 129 条记录并停止。

Me.Recordset.MoveNext

Trying to find an instruction that will go to the next record untill it reaches the End of File. I am using Access 2010 (Access 2002 -2003 file format mdb) as the front end. The recordsource is a SQL Server 2008 linked View.

试图找到一条指令,该指令将转到下一条记录,直到到达文件尾。我使用 Access 2010(Access 2002 -2003 文件格式 mdb)作为前端。记录源是 SQL Server 2008 链接视图。

回答by codeape

To loop from current record to the end:

从当前记录循环到最后:

While Me.CurrentRecord < Me.Recordset.RecordCount
    ' ... do something to current record
    ' ...

    DoCmd.GoToRecord Record:=acNext
Wend

To check if it is possible to go to next record:

要检查是否可以转到下一条记录:

If Me.CurrentRecord < Me.Recordset.RecordCount Then
    ' ...
End If

回答by fingerman

If (Not IsNull(Me.id.Value)) Then
DoCmd.GoToRecord , , acNext
End If

Hi, you need to put this in form activate, and have an id field named id...

嗨,您需要将其放入表单 activate 中,并有一个名为 id 的 id 字段...

this way it passes until it reaches the one without id (AKA new one)...

这样它就会通过,直到它到达没有 id 的那个(又名新的)......

回答by David-W-Fenton

I have done this in the past, and have always used this:

我过去这样做过,并且一直使用这个:

  With Me.RecordsetClone
    .MoveFirst
    Do Until .EOF
      If Me.Dirty Then
         Me.Dirty = False
      End If
      .MoveNext
      Me.Bookmark = .Bookmark
    Loop
  End With

Some people would use the form's Recordset, which doesn't require setting the bookmark (i.e., navigating the form's Recordset navigates the form's edit buffer automatically, so the user sees the move immediately), but I prefer the indirection of the RecordsetClone.

有些人会使用窗体的 Recordset,它不需要设置书签(即,导航窗体的 Recordset 会自动导航窗体的编辑缓冲区,因此用户会立即看到移动),但我更喜欢 RecordsetClone 的间接性。

回答by Peter Taylor

If you want cmd buttons that loop through the form's records, try adding this code to your cmdNext_Clickand cmdPrevious_ClickVBA. I have found it works well and copes with BOF / EOF issues:

如果你想CMD按钮,遍历窗体的记录,你可以添加以下代码到你cmdNext_ClickcmdPrevious_ClickVBA。我发现它运行良好并且可以处理 BOF / EOF 问题:

On Error Resume Next

DoCmd.GoToRecord , , acNext

On Error Goto 0


On Error Resume Next

DoCmd.GoToRecord , , acPrevious

On Error Goto 0

Good luck! PT

祝你好运!PT

回答by Patrick Honorez

Set rs = me.RecordsetClone
rs.Bookmark = me.Bookmark
Do
    rs.movenext
Loop until rs.eof

回答by Ruud

Keeping the code simple is always my advice:

保持代码简单始终是我的建议:

If IsNull(Me.Id) = True Then
  DoCmd.GoToRecord , , acNext
Else
  DoCmd.GoToRecord , , acLast
End If

回答by sajid

Add This Code on Form Close Event whether you add new record or delete, it will recreate the Primary Keys from 1 to Last record.This code will not disturb other columns of table.

在表单关闭事件中添加此代码无论您添加新记录还是删除记录,它都会重新创建从 1 到最后一条记录的主键。此代码不会干扰表的其他列。

Sub updatePrimaryKeysOnFormClose()
    Dim i, rcount As Integer
    'Declare some object variables
    Dim dbLib As Database
    Dim rsTable1 As Recordset
    'Set dbLib to the current database (i.e. LIBRARY)
    Set dbLib = CurrentDb
    'Open a recordset object for the Table1 table
    Set rsTable1 = dbLib.OpenRecordset("Table1")
    rcount = rsTable1.RecordCount
    '== Add New Record ============================  
        For i = 1 To rcount
              With rsTable1
                      rsTable1.Edit  
                      rsTable1.Fields(0) = i  
                      rsTable1.Update  
                      '-- Go to Next Record ---  
                      rsTable1.MoveNext
             End With  
        Next  
        Set rsTable1 = rsTable1
End Sub