I need to have my program be able to search each column on a database until it finds matches, then output those matches into a combo box. I have currently programmed a way of it searching one column (specifically the name of an animal) but I need to add the option of searching other columns and displaying multiple results in a combo box that the user can then choose to open with the program into Form 2, which I use to display the data.
Here is my current code that I am using, the first part is for searching by the animal name, the second part is for searching in the optional box which then needs to display the results into the combo box. I also have the issue where I cannot search in the optional box unless the name box has data in it, which defeats the purpose of the optional box. Any help will be greatly appreciated.
Here is my current code that I am using, the first part is for searching by the animal name, the second part is for searching in the optional box which then needs to display the results into the combo box. I also have the issue where I cannot search in the optional box unless the name box has data in it, which defeats the purpose of the optional box. Any help will be greatly appreciated.
Code:
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