从哪里选择包含

时间:2017-11-02 22:43:46

标签: ms-access contains

我有一个数据库,我可以添加一个人的全名,我正在尝试使用textBox和一个按钮来实现搜索功能,但我只想搜索第一个或最后一个名字,而不是必须输入完整的名。

我尝试使用SELECT FROM WHERE CONTAINS,如下所示:

OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Table WHERE CONTAINS (column, '"+textBox.Text+"')";

但我一直收到这个错误:

Syntax error (missing operator) in query expression 'CONTAINS (column,'the text i tried to search')'.

我也尝试将+更改为%或*或&但它仍然没有用。

3 个答案:

答案 0 :(得分:1)

包含无效Access SQL。使用

cmd.CommandText = "SELECT * FROM Table WHERE [YourNameField] Like '*" + textBox.Text + "*')";

答案 1 :(得分:0)

以下是您想要的搜索示例:

Private Sub cmdFind_DisplayName_Click()
    Dim dbs As Database, rstPatient As Recordset
    Dim txtDisplayName, strQuote As String
    strQuote = Chr$(34)
    On Error GoTo ErrorHandler

    Me.OrderBy = "DISPLAYNAME"
    Me.OrderByOn = True

    Set dbs = CurrentDb
    Set rstPatient = Me.RecordsetClone
    txtDisplayName = Trim(InputBox("Please Enter Patient Name ", "Patient Find By Name"))
    txtDisplayName = UCase(txtDisplayName) & "*"
    If IsNull(txtDisplayName) Then
        MsgBox ("No Patient Name Entered - Please Enter a Valid Patient Name")
    Else
        rstPatient.FindFirst "[DISPLAYNAME] Like " & strQuote & txtDisplayName & strQuote
        If Not (rstPatient.NoMatch) Then
            Me.Bookmark = rstPatient.Bookmark
            Me.Refresh
        Else
            MsgBox ("Patient Not Found - Please Enter a New Patient Name")
        End If
    End If

    GoTo Exit_cmdFind_Click

    ErrorHandler:
        MsgBox LTrim(RTrim(Me.NAME)) + "." + "Patient Find By Display Name - " + "Error: " + AccessError(Err.Number)

    Exit_cmdFind_Click:
      rstPatient.Close
      Set dbs = Nothing
      Set rstPatient = Nothing
End Sub

答案 2 :(得分:0)



创建1个文本框(txtMain)和搜索命令按钮(btnSearch)以执行SQL。然后添加一个列表框(listResult)来显示结果。

Private Sub btnSearch_Click()

Dim mainSQL As String

mainSQL = " SELECT YOUR_FIELD_NAME " & _
      " FROM MasterReg " & _
      " WHERE Left(,InStr(YOUR_FULL_NAME_FIELD,' ')-1) LIKE '" &  me.txtMain & "*'"  & _ ' Firstname Search
      " OR RIGHT( YOUR_FULL_NAME_FIELD,Len( YOUR_FULL_NAME_FIELD )-InStr( YOUR_FULL_NAME_FIELD,' ')) LIKE '" &  me.txtMain & "*'" 'Surname Search                   

Me.listResult.SetFocus               
Me.listResult.RowSource = mainSQL
Me.listResult.Requery

End Sub