我的大学项目我正在设计一个程序,可以读取有关动物的Microsoft Access数据库并显示数据。我设法编程它,以便我可以搜索动物名称,它将以另一种形式显示记录,但我需要能够搜索其他字段,如LatinName或AverageWeight,然后显示任何匹配的结果然后,用户可以选择一个组合框,程序将显示代码,例如如果我要输入50公斤并且有两个50公斤的记录,它会显示它们两个然后允许我选择我想要的那个。
非常感谢任何帮助或建议,随时可以询问您是否需要更多信息。
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
答案 0 :(得分: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语句即可。
答案 1 :(得分:0)
根据NiteTrip的回答,我相信您还需要将表单的KeyPreview属性设置为TRUE,以便它可以获取按键。