0

一致するものが見つかるまでプログラムでデータベースの各列を検索し、それらの一致をコンボ ボックスに出力できるようにする必要があります。現在、1 つの列 (具体的には動物の名前) を検索する方法をプログラムしていますが、他の列を検索し、ユーザーがプログラムで開くことを選択できるコンボ ボックスに複数の結果を表示するオプションを追加する必要があります。データの表示に使用するフォーム 2。

これが私が使用している現在のコードです。最初の部分は動物の名前で検索するためのもので、2番目の部分はオプションボックスで検索するためのもので、結果をコンボボックスに表示する必要があります。また、名前ボックスにデータが含まれていないとオプション ボックスを検索できないという問題もあります。これは、オプション ボックスの目的を無効にします。どんな助けでも大歓迎です。

 Private Sub btnsear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsear.Click
    If (txtname.Text = "'") Then
        MsgBox("No information has been entered")
    Else
        Try
            Dim newsql As String
            newsql = "select * from Animals where AnimalName like " & "'%" & txtname.Text & "%'"
            'MsgBox("select * from Animals where AnimalName like " & "'" & txtname.Text & "'")
            'MsgBox(newsql)
            Dim con As New OleDb.OleDbConnection
            Dim da As New OleDb.OleDbDataAdapter

            'Dim ds As NewDataTable
            Dim dt As New DataTable("Animals")
            'uses the 2010 compatible connection string
            con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Kamran\Desktop\College\Computing Project\Animals.accdb" 'h:\Animals.accdb"
            con.Open()

            da = New OleDb.OleDbDataAdapter(newsql, con)
            da.Fill(dt)

            Form2.Show()

            'show name in unbound text box
            Form2.nametxt.Text = dt.Rows(0).Item(1)
            Form2.latintxt.Text = dt.Rows(0).Item(2)
            Form2.locationtxt.Text = dt.Rows(0).Item(3)
            Form2.heighttxt.Text = dt.Rows(0).Item(4)
            Form2.weighttxt.Text = dt.Rows(0).Item(5)
            Form2.diettxt.Text = dt.Rows(0).Item(6)
            Form2.statustxt.Text = dt.Rows(0).Item(7)
            Form2.lifetxt.Text = dt.Rows(0).Item(8)
            Form2.breedtxt.Text = dt.Rows(0).Item(9)
            Form2.lengthtxt.Text = dt.Rows(0).Item(10)
            Form2.txtimage.Text = dt.Rows(0).Item(11)

        Catch
            'MsgBox("Animal Not Found")
            'con.close()
        End Try
    End If


    If txtname.Text = "" Then
        MsgBox("Invalid Search")
    Else
        Try
            Dim newsql As String = "SELECT * FROM Animals WHERE AnimalName LIKE " & "'%" & txtname.Text & "%'"

            If txtopt.Text <> "" Then

                newsql &= " AND (AnimalName LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR LatinName LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR Location LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageHeight LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageWeight LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR DietaryNeeds LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR ConservationStatus LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageLifeSpan LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR BreedingSeason LIKE " & "'%" & txtopt.Text & "%'" & _
                          " OR AverageLength LIKE " & "'%" & txtopt.Text & "%')"
            End If

            Dim con As New OleDb.OleDbConnection
            Dim da As New OleDb.OleDbDataAdapter

            Dim dt As New DataTable("Animals")

            con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\Kamran\Desktop\College\Computing Project\Animals.accdb" 'h:\Animals.accdb"
            con.Open()

            da = New OleDb.OleDbDataAdapter(newsql, con)
            da.Fill(dt)
    Catch
            'MsgBox("Item Not Found")
            'con.close()
        End Try
    End If
4

1 に答える 1

0

2 番目のテキスト ボックスにもユーザーからのクエリ データが含まれている場合は、最初のフィールドで行ったのと同じように、それを SQL ステートメントに追加します。 AND Field2 like %User Value%

于 2013-04-14T23:33:48.147 に答える