0

对于我的大学项目,我正在设计一个程序,该程序可以读取有关动物的 Microsoft Access 数据库并显示数据。我已经设法对其进行了编程,以便我可以按动物名称进行搜索,它会以另一种形式显示记录,但我需要能够搜索其他字段,例如 LatinName 或 AverageWeight,然后显示任何匹配的结果进入一个组合框,然后用户可以选择该组合框,程序将显示代码,例如,如果我要输入 50kg 并且有两条 50kg 的记录,它会同时显示它们,然后让我选择我想要的。

任何帮助或建议将不胜感激,如果您需要更多信息,请随时询问。

Public Class Form1

Private Sub btnsear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsear.Click
    If (txtname.Text = "") Then
        MsgBox("Invalid Search")
    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 = 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(9)
            Form2.breedtxt.Text = dt.Rows(0).Item(10)
            Form2.lengthtxt.Text = dt.Rows(0).Item(11)
            Form2.txtimage.Text = dt.Rows(0).Item(12)
            Form2.socialchk.Checked = dt.Rows(0).Item(8)

            If dt.Rows(0).Item(8) = True Then
                Form2.socialchk.Checked = True
            Else
                Form2.socialchk.Checked = False
            End If

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

    End If

    If (txtopt.Text = "'") Then
        Try
            Dim newsql As String
            newsql = "select * from Animals where AnimalName like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where LatinName like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where Location like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where AverageHeight like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where AverageWeight like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where DietaryNeeds like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where ConservationStatus like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where AverageLifeSpan like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where BreedingSeason like " & "'%" & txtopt.Text & "%'"
            newsql = "select * from Animals where AverageLength like " & "'%" & txtopt.Text & "%'"

        Catch
        End Try
    End If

End Sub
4

2 回答 2

0

根据 NiteTrip 的回答,我相信您还需要将表单的 KeyPreview 属性设置为 TRUE,以便它能够拾取按键。

于 2013-02-22T15:54:17.597 回答
0

因此,您以空白文本框开始表单。您希望能够通过在其中输入内容来搜索任何文本框。您应该使用多个句柄从文本框中获取输入,例如:

 Private Sub searchByKeydown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles nameTxt.KeyDown, latintxt.KeyDown, locationtxt.KeyDown, heighttxt.KeyDown ' and all the rest of your textbox names
    If e.KeyCode = Keys.Return Then ' This checks if the return key was pressed and will start the search
        Dim tmpText As TextBox = DirectCast(sender, TextBox) ' grab the textbox that triggered the event
        If tmpText.text = String.Empty Then ' enter was pressed without a search term
            MsgBox("Please enter a search term")
            Exit Sub
        End If
        Dim strSearchQuery As String = tmpText.text ' This is what the user wants to search
        Select Case tmpText.name
           Case "nameTxt"
                ' put in your search routine here for this textbox
           Case "latintxt"
                ' put in your search routine here for this textbox
           Case "locationtxt"
                ' put in your search routine here for this textbox
           Case "heighttxt"
                ' put in your search routine here for this textbox
         End Select
    End If
  End Sub

你会有更多的案例陈述,但这应该会让你继续前进。由于您已经可以按名称搜索,因此只需更改其他搜索的 SQL 语句即可。

于 2013-02-21T02:39:27.510 回答