I have created a program which displays data from different tables in an access database(without the use of wizards to connect to the database. i.e. all connection have been hard coded), I have created a simple search which searches through one field in one table. However I would like to make a function which allows me to search through all of the fields in a table. Below is the simple search function I have created.
Public Sub Search()
con.Open()
Dim dt As New DataTable("Table1")
Dim rs As New OleDb.OleDbDataAdapter("Select * from Table1 where FirstName = '" & txtTabel1.Text & "'", con)
rs.Fill(dt)
dgvTabel2.DataSource = dt
dgvTabel2.Refresh()
rs.Dispose()
con.Close()
end sub
The new function should look something like this
Public Sub SearchHard(TableName As String)
con.Open()
Dim dt As New DataTable("TableName")
Dim rs As New OleDb.OleDbDataAdapter(("Select * from " & TableName & " where FirstName = '" & txtTabel1.Text & "'") Or ("Select * from " & TableName & " where LastName = '" & txtTabel1.Text & "')"), con)
'SELECT * FROM MyTable WHERE FirstName LIKE '% txtTable1.text %' OR LastName LIKE '%txtTable1.tetx%'
rs.Fill(dt)
dgvTabel1.DataSource = dt
dgvTabel1.Refresh()
rs.Dispose()
con.Close()
End Sub
It would also be a big plus if the function could accept a parameter to select a different table to reduce overusing the code. Any and all help would be much appreciated
Working code to with searches.
con.Open()
Dim dt As New DataTable("Table1")
Dim rs As New OleDb.OleDbDataAdapter("SELECT * FROM Table1 WHERE (FirstName = '" & txtTabel1.Text & "') or (LastName = '" & txtTabel1.Text & "')", con)
rs.Fill(dt)
dgvTabel1.DataSource = dt
dgvTabel1.Refresh()
rs.Dispose()
con.Close()