无法从记录集中找到下一条记录

时间:2014-03-21 11:58:16

标签: vba recordset

我正在编码搜索功能,根据输入的选定字段和值搜索记录。我正在使用findfirst和findnext方法,但是,我只能找到第一条记录而无法让查找下一个功能。以下是我的代码:

     Set rs = Forms("tabel1").Recordset.Clone

        sField = Switch(field = "IDs", "ID", field = "First Name", "FirstName", field = "Last Name", "LastName", field = "Email Address", "Email", True, "OTHER")

        If count > 1 Then
            rs.FindNext (sField & " = '" & value & "'")
            If rs.NoMatch Then
                MsgBox ("There are no more instances of '" & value & "' in" & sField & ".")
                count = 1
                rs.Close
                Exit Sub
            Else
                ' Find the next instance
                Forms("tabel1").Bookmark = rs.Bookmark
            End If
        Else
            rs.FindFirst (sField & " = '" & value & "'")
              If rs.NoMatch Then
                MsgBox ("Cannot find '" & value & "' in" & sField & "; Please enter a valid value.")
                rs.Close
                Exit Sub
              Else
                Forms("tabel1").Bookmark = rs.Bookmark
            End If
        End If
        Forms("tabel1").Refresh
        rs.Close
        Else
        MsgBox ("Please enter a valid search value")
        Me.txtValue.SetFocus
    End If
    Else
    Me.txtValue.SetFocus
    Me.txtValue.Text = ""
    answer = MsgBox("Please select a search field and enter a search value.", vbExclamation, "Missing Fields")
    Me.ComboSearchField.SetFocus
End If

请告诉我我错过了什么或做错了什么。对不起,我在VBA中很生气。 谢谢。

1 个答案:

答案 0 :(得分:1)

您实际上没有在提供的代码中循环,因此您将始终在第一遍中点击FindFirst,并且永远不会回复FindNext语句。尝试这样的事情:

    Do Until rs.EOF
        If rs.AbsolutePosition = 1 Then
            'do something to first record set
        Else
            'do something else...
        End If
    Loop

Do Until rs.EOF只会在您返回记录集时循环播放。