2013-02-20 84 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,以便它能夠按下按鍵。