2013-02-20 85 views
0

我的大学项目我正在设计一个程序,可以读取关于动物的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 
+1

您正在显示记录的位置。在数据网格视图? – Arpit 2013-02-20 17:46:41

+0

不,我把它们放在另一个表单上的文本框中,因为我觉得这将是向用户呈现数据的最佳方式。 – Silver 2013-02-20 19:50:04

回答

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 

你将有更多的case语句,但这应该让你去。由于您已经可以按名称进行搜索,因此只需更改其他搜索的SQL语句即可。

0

Per NiteTrip的回答,我相信你还需要将表单的KeyPreview属性设置为TRUE,以便它能够按下按键。