使用多个条件进行搜索

时间:2014-09-18 15:17:19

标签: sql vba ms-access access-vba

我有两个要搜索表格的文本框。我正在通过一个我认为是问题的查询进行搜索。代码应该从两个文本框中获取标准并搜索一个表。之后,其余的文本框将填充最接近搜索的记录集。

我在RstRecSet.MoveLast收到错误我收到一个没有当前记录的编译错误。我确实有一个记录,就像我在盒子里输入的那样,虽然

没有拿起它

当我添加记录的确切名称时,它发现没问题。它几乎就好像它对待类似的算子一样,好像它是一个=。

这是我的代码:

Private Sub Command514_Click()
'DoCmd.Close
'DoCmd.OpenForm "frmContacts", acNormal
    Set RstRecSet = Nothing
    Set db = CurrentDb
    Dim searchNum As String
    Dim searchName As String
    searchNum = txtGroupNr
    searchName = txtGroupName
 On Error Resume Next
If IsNull(txtGroupNr) Or txtGroupNr = "" Then

        Me.txtGroupName.BackColor = vbRed
        Forms!frmGroupHeader!txtGroupNr.SetFocus

    Else
        'strSearchICN = txtGroupNr
        Set db = CurrentDb
        Me.txtGroupName.BackColor = vbWhite
        Set RstRecSet = db.OpenRecordset("Select * from tblGroupHeader Where GroupNum  Like '" & searchNum & "' And GroupName  Like '" & searchName & "';", dbOpenDynaset)
        RstRecSet.MoveLast
        intMaxCount = RstRecSet.RecordCount
        RstRecSet.MoveFirst
      ' Exit Sub
   End If

     If RstRecSet.EOF Then


        Me.txtGroupName.BackColor = vbRed
        Forms!frmGroupHeader!txtGroupNr.SetFocus

    Else
       Call DisplayFields

    End If
End Sub

以下是解决方案

    Private Sub Command514_Click()
'DoCmd.Close
'DoCmd.OpenForm "frmContacts", acNormal
    Set RstRecSet = Nothing
    Set db = CurrentDb
    Dim searchGroup As String
    Dim searchName As String

If IsNull(txtgroupSearch) Or txtgroupSearch = "" Then

        Me.txtGroupName.BackColor = vbRed
        Forms!frmGroupHeader!txtGroupNr.SetFocus

    Else
        'searchNum = txtGroupNr
        searchGroup = txtgroupSearch

        Set db = CurrentDb
        Me.txtGroupName.BackColor = vbWhite
        Set RstRecSet = db.OpenRecordset("Select * from tblGroupHeader Where groupName like '*" & searchGroup & "*' or groupNum like '*" & searchGroup & "*';", dbOpenDynaset)
            If RstRecSet.EOF And RstRecSet.BOF Then
            MsgBox ("NO RECORDS!")
                Exit Sub
            End If
        RstRecSet.MoveLast
        intMaxCount = RstRecSet.RecordCount
        RstRecSet.MoveFirst
      ' Exit Sub
   End If

     If RstRecSet.EOF Then
        Me.txtGroupName.BackColor = vbRed
        Forms!frmGroupHeader!txtGroupNr.SetFocus

    Else
        Call DisplayFields

    End If
End Sub

1 个答案:

答案 0 :(得分:1)

我认为问题出在你的SQL中。该错误表明您没有记录。在记录集上调用.MoveLast之前,您可以使用以下命令进行快速测试:

If RstRecSet.EOF and RstRecSet.BOF Then 
    msgbox("NO RECORDS!")
    exit sub
End if

回到SQL。您写了"Select * from tblGroupHeader Where GroupNum and GroupName Like '" & searchNum & GroupName & "';",如果您选择“1”作为GroupNum而“物理”作为组名,则会出现类似Select * from tblGroupHeader Where GroupNum and GroupName Like '1Physics';这样的内容

相反:"Select * from tblGroupHeader Where GroupNum Like '" & searchNum & "' and GroupName Like '" & GroupName & "';"看起来像Select * from tblGroupHeader Where GroupNum Like '1' and GroupName Like 'Physics';

我发现首先将我的SQL放入变量然后使用Debug.Print SQL将该变量写入Immediates窗口是有帮助的,这样我就可以复制并粘贴回我的数据库以确保所有内容都有效感。