使用 VBA 循环记录集

发布于 2024-08-28 10:22:13 字数 1946 浏览 9 评论 0原文

我试图按以下方式以循环方式将销售人员 (rsSalespeople) 分配给客户 (rsCustomers):

  1. 导航到第一个客户,将第一个销售人员分配给客户。
  2. 移至下一个客户。如果 rsSalesPersons 不在 EOF,则移至下一个 SalesPerson;如果 rsSalesPersons 位于 EOF,则 MoveFirst 循环回第一个 SalesPerson。将此(当前)销售人员分配给(当前)客户。
  3. 重复步骤 2,直到 rsCustomers 位于 EOF(EOF = True,即 End-Of-Recordset)。

自从我处理 VBA 以来已经有一段时间了,所以我有点生疏了,但到目前为止,这是我想到的:

Private Sub Command31_Click()

'On Error GoTo ErrHandler

Dim intCustomer As Integer
Dim intSalesperson As Integer
Dim rsCustomers As DAO.Recordset
Dim rsSalespeople As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT CustomerID, SalespersonID FROM Customers WHERE SalespersonID Is Null"
Set rsCustomers = CurrentDb.OpenRecordset(strSQL)

strSQL = "SELECT SalespersonID FROM Salespeople"
Set rsSalespeople = CurrentDb.OpenRecordset(strSQL)

rsCustomers.MoveFirst
rsSalespeople.MoveFirst

Do While Not rsCustomers.EOF

    intCustomer = rsCustomers!CustomerID
    intSalesperson = rsSalespeople!SalespersonID

    strSQL = "UPDATE Customers SET SalespersonID = " & intSalesperson & " WHERE CustomerID = " & intCustomer
    DoCmd.RunSQL (strSQL)
    rsCustomers.MoveNext

    If Not rsSalespeople.EOF Then
        rsSalespeople.MoveNext
    Else
        rsSalespeople.MoveFirst
    End If

Loop

ExitHandler:
    Set rsCustomers = Nothing
    Set rsSalespeople = Nothing
    Exit Sub

ErrHandler:
    MsgBox (Err.Description)
    Resume ExitHandler

End Sub

我的表是这样定义的:

Customers
--CustomerID
--Name
--SalespersonID

Salespeople
--SalespersonID
--Name

对于 10 个客户和 5 个销售人员,我的预期结果是这样的:

CustomerID--Name--SalespersonID
1---A---1
2---B---2
3---C---3
4---D---4
5---E---5
6---F---1
7---G---2
8---H---3
9---I---4
10---J---5

上面的代码适用于 Salespeople 记录集的初始循环,但在找到记录集末尾时会出错。无论 EOF 如何,它似乎仍然尝试执行 rsSalespeople.MoveFirst 命令。

我没有正确检查 rsSalespeople.EOF 吗?有什么想法可以让这段代码正常工作吗?

I am trying to assign salespeople (rsSalespeople) to customers (rsCustomers) in a round-robin fashion in the following manner:

  1. Navigate to first Customer, assign the first SalesPerson to the Customer.
  2. Move to Next Customer. If rsSalesPersons is not at EOF, move to Next SalesPerson; if rsSalesPersons is at EOF, MoveFirst to loop back to the first SalesPerson. Assign this (current) SalesPerson to the (current) Customer.
  3. Repeat step 2 until rsCustomers is at EOF (EOF = True, i.e. End-Of-Recordset).

It's been awhile since I dealt with VBA, so I'm a bit rusty, but here is what I have come up with, so far:

Private Sub Command31_Click()

'On Error GoTo ErrHandler

Dim intCustomer As Integer
Dim intSalesperson As Integer
Dim rsCustomers As DAO.Recordset
Dim rsSalespeople As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT CustomerID, SalespersonID FROM Customers WHERE SalespersonID Is Null"
Set rsCustomers = CurrentDb.OpenRecordset(strSQL)

strSQL = "SELECT SalespersonID FROM Salespeople"
Set rsSalespeople = CurrentDb.OpenRecordset(strSQL)

rsCustomers.MoveFirst
rsSalespeople.MoveFirst

Do While Not rsCustomers.EOF

    intCustomer = rsCustomers!CustomerID
    intSalesperson = rsSalespeople!SalespersonID

    strSQL = "UPDATE Customers SET SalespersonID = " & intSalesperson & " WHERE CustomerID = " & intCustomer
    DoCmd.RunSQL (strSQL)
    rsCustomers.MoveNext

    If Not rsSalespeople.EOF Then
        rsSalespeople.MoveNext
    Else
        rsSalespeople.MoveFirst
    End If

Loop

ExitHandler:
    Set rsCustomers = Nothing
    Set rsSalespeople = Nothing
    Exit Sub

ErrHandler:
    MsgBox (Err.Description)
    Resume ExitHandler

End Sub

My tables are defined like so:

Customers
--CustomerID
--Name
--SalespersonID

Salespeople
--SalespersonID
--Name

With ten customers and 5 salespeople, my intended result would like like:

CustomerID--Name--SalespersonID
1---A---1
2---B---2
3---C---3
4---D---4
5---E---5
6---F---1
7---G---2
8---H---3
9---I---4
10---J---5

The above code works for the intitial loop through the Salespeople recordset, but errors out when the end of the recordset is found. Regardless of the EOF, it appears it still tries to execute the rsSalespeople.MoveFirst command.

Am I not checking for the rsSalespeople.EOF properly? Any ideas to get this code to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-09-04 10:22:13

rsSalespeople.EOF 并不指示您何时位于最后一行,而是指示您何时超过最后一行。

因此,当您的条件命中最后一个销售人员 EOF 为 false 时,它​​会执行 movenext (使 EOF 为 true),然后循环的下一次遍历将在 rsSalespeople 的“EOF 行”上进行操作,您无法从中提取值,因此会出现错误。

试试这个:

rsSalespeople.MoveNext
If (rsSalespeople.EOF) Then
    rsSalespeople.MoveFirst
End If

rsSalespeople.EOF doesn't indicate when you are on the last row, it indicates when you are PAST the last row.

So when your conditional hits the last salesperson EOF is false so it does a movenext (making EOF true) then the next pass through the loop is operating on the "EOF row" of rsSalespeople which you can't pull values from, hence the error.

Try this instead:

rsSalespeople.MoveNext
If (rsSalespeople.EOF) Then
    rsSalespeople.MoveFirst
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文