如何使用find next方法搜索记录

时间:2014-06-22 06:31:23

标签: vba ms-access-2010

我有多个具有相同客户编号的记录,我使用查找下一个方法搜索下一条记录,客户编号相同。我的代码只搜索第二条记录,而不是第三次或第四次搜索相同的客户编号。下面是代码可以请你帮忙

Private Sub Command114_Click()
    Dim db As dao.Database
    Dim rs1 As dao.Recordset
    Dim pn As Long
    Set db = CurrentDb()
    Set rs1 = db.OpenRecordset("Application", dbOpenDynaset)
    If (Text85 & vbNullString) = vbNullString Then
        MsgBox "Please enter the Account no/CIF"
    Else
        pn = Me.Text85.Value
        rs1.FindNext "[Cus_Number] = " & pn
        If rs1.NoMatch Then
            MsgBox ("Sorry The Accountno/CIF is not found")
        Else
            Me.S_No = rs1.Fields("sno").Value
            Me.Cus_Name = rs1.Fields("Cus_Name").Value
            Me.App_level1 = rs1.Fields("App_level1").Value
            Me.App_level2 = rs1.Fields("App_level2").Value
            Me.App_level3 = rs1.Fields("App_level3").Value
            Me.Dec_level1 = rs1.Fields("Dec_level1").Value
            Me.Dec_level2 = rs1.Fields("Dec_level2").Value
            Me.Dec_level3 = rs1.Fields("Dec_level3").Value
            Me.Com_level1 = rs1.Fields("Com_level1").Value
            Me.Com_level2 = rs1.Fields("Com_level2").Value
            Me.Com_level3 = rs1.Fields("Com_level3").Value
            Me.Date1 = rs1.Fields("Date1").Value
            Me.Date2 = rs1.Fields("Date2").Value
            Me.Date3 = rs1.Fields("Date3").Value
        End If
    End If
        rs1.FindNext "[Cus_Number] = " & pn
        Set rs1 = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

我假设您想要的功能是将所有实例(2,3,4等)更改为输入的值。删除它(结束后的那个,在结束后):

rs1.FindNext "[Cus_Number] = " & pn

并将FindNext放入一个循环中,该循环将继续查找和更新您的记录,直到有NoMatch:

    rs1.FindNext "[Cus_Number] = " & pn

    If rs1.NoMatch Then
        MsgBox ("Sorry The Accountno/CIF is not found")
    Else
        Do Until rs1.NoMatch
            Me.S_No = rs1.Fields("sno").Value
            Me.Cus_Name = rs1.Fields("Cus_Name").Value
            Me.App_level1 = rs1.Fields("App_level1").Value
            Me.App_level2 = rs1.Fields("App_level2").Value
            Me.App_level3 = rs1.Fields("App_level3").Value
            Me.Dec_level1 = rs1.Fields("Dec_level1").Value
            Me.Dec_level2 = rs1.Fields("Dec_level2").Value
            Me.Dec_level3 = rs1.Fields("Dec_level3").Value
            Me.Com_level1 = rs1.Fields("Com_level1").Value
            Me.Com_level2 = rs1.Fields("Com_level2").Value
            Me.Com_level3 = rs1.Fields("Com_level3").Value
            Me.Date1 = rs1.Fields("Date1").Value
            Me.Date2 = rs1.Fields("Date2").Value
            Me.Date3 = rs1.Fields("Date3").Value

            rs1.FindNext "[Cus_Number] = " & pn
        Loop
    End If

但总的来说,我不确定你要做什么。您是否希望使用表单上的最新信息更新记录集?您拥有的代码将使用记录集中最后一组找到的值覆盖表单上的当前值。我本以为你想要相反......