如何循环遍历子窗体的记录,VBA Access 2010

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

how to loop through the records of a subform, VBA Access 2010

vba

提问by Paul John

Here's my question: i have a subform inside a main form. The main form contains the customer number, and the subform contains the order for that customer. So one customer may have multiple orders. I want to create a buttom which when clicked moves to the next order for that customer. I have no issue doing this with the code:

这是我的问题:我在主窗体中有一个子窗体。主表单包含客户编号,子表单包含该客户的订单。所以一个客户可能有多个订单。我想创建一个按钮,单击该按钮后会移动到该客户的下一个订单。我对代码没有问题:

 Private Sub next_comp_Click()
 [Forms]![Customer]![Order Subform].SetFocus
 DoCmd.GoToRecord , , acNext
  end sub

where I have issues is when I get to the last order for that customer. I will like it to loop back to the first order. To do this i used this code:

我遇到问题的地方是当我到达该客户的最后一个订单时。我会喜欢它循环回到第一个顺序。为此,我使用了以下代码:

Private sub next_comp_click()
[Forms]![Customer]![Compressor Subform].SetFocus
Dim dbcustomer As Database
Dim rstorder As Recordset

Set dbcustomer = CurrentDb
Set rstorder= dbcustomer.OpenRecordset("orders", dbOpenSnapshot)
  With rstEmployees
    If .AbsolutePosition = - 1 Then
    MsgBox (.RecordCount)
    'DoCmd.GoToRecord , , acFirst
    Else
    DoCmd.GoToRecord , , acNext
    End If
  End With
 end sub

I used the MsgBox function to kind of debug to figure what record I am on (ultimately I want it to loop back to the first record as shown in the commented out code), but for some reason, it tells me i am always on the first record, i.e. it tells me the RecordCount is 1. This is not supposed to be so because most of the records I am working with have multiple orders. If I change the condition to something like If .AbsolutePosition = 3, it moves the records to the next until i get to the last or empty record and if i click next from there, it gives me the run-time error '2105' you cannot go to the specified record.

我使用 MsgBox 函数进行调试以找出我所在的记录(最终我希望它循环回到注释掉的代码中所示的第一条记录),但出于某种原因,它告诉我我总是在第一条记录,即它告诉我 RecordCount 是 1。这不应该是这样,因为我正在处理的大多数记录都有多个订单。如果我将条件更改为 If .AbsolutePosition = 3 之类的内容,它将记录移动到下一个,直到我到达最后一个或空记录,如果我从那里单击下一步,它会给我运行时错误“2105”你无法转到指定的记录。

I think the issue is the .AbsolutePositon property is not looking at the right form or table, but any answers will be much appreciated.

我认为问题是 .AbsolutePositon 属性没有查看正确的表格或表格,但任何答案都将不胜感激。

Thank you in advance.

先感谢您。

回答by ObieMD5

You do not even need that recordset stuff if your forms are bounded. Just use the following code in the button:

如果您的表单是有界的,您甚至不需要那些记录集的东西。只需在按钮中使用以下代码:

Private sub next_comp_click()
   [Forms]![Customer]![Order Subform].SetFocus
   If [Forms]![Customer]![Order Subform].Form.CurrentRecord >= [Forms]![Customer]![Order Subform].Form.Recordset.RecordCount Then
       DoCmd.GoToRecord , , acFirst
   Else
       DoCmd.GoToRecord , , acNext
   End If
End sub

I built an access database to replicate this just to make sure the code was exact. You should be able to copy and paste this and it will work.

我建立了一个访问数据库来复制这个只是为了确保代码是准确的。您应该能够复制和粘贴它,它会起作用。

回答by Andrew

This is what I do to loop to the First record after hitting the last record. Put it in VBA. Just do the reverse if using a previous button.

这是我在点击最后一条记录后循环到第一条记录的方法。把它放在VBA中。如果使用上一个按钮,只需执行相反的操作。

 If Me.CurrentRecord = Me.Recordset.RecordCount Then
       DoCmd.GoToRecord , , acFirst
   Else
       DoCmd.GoToRecord , , acNext
   End If