Hello,
I'm currently doing my project in VB and connect with access that have three tables in database.
I have three buttons to show three different table in my database. I'm using datagrid and when I click on radio button 1st table, it will show the data in my 1st table. I also include search button whereby I can filter data base on name or id number. But I couldn't filter the data, when I try to filter by name, it will closed.
here's my code
'Show Data in DataGrid
Private Sub radpdbtn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radpdbtn.CheckedChanged
Dim schomasys As New DataTable()
dataAdapter = New OleDbDataAdapter("SELECT * FROM PersonalDetails", conn)
dataAdapter.Fill(schomasys)
dgrid.DataSource = schomasys
End Sub
Private Sub radedubtn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radedubtn.CheckedChanged
Dim schomasys As New DataTable()
dataAdapter = New OleDbDataAdapter("SELECT * EducationRecords", conn)
dgrid.DataSource = schomasys
End Sub
Private Sub radpbtn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radpbtn.CheckedChanged
Dim schomasys As New DataTable()
dataAdapter = New OleDbDataAdapter("SELECT * FROM payment", conn)
dataAdapter.Fill(schomasys)
dgrid.DataSource = schomasys
End Sub
Private Sub searchpbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchbtn.Click
connection.Open()
If radpdbtn.Checked = True Then
' SQL Statement to search for either Name or ID Number
Dim sqlsearch As String
sqlsearch = "SELECT * FROM PersonalDetails"
Try
If Me.combosearch.Text = "Name" Then
sqlsearch = sqlsearch & " where sname like '%" & Me.txtsearch.Text
Else
sqlsearch = sqlsearch & " where idno =" & Me.txtsearch.Text
End If
' Once again we execute the SQL statements against our DataBase
Dim adapter As New OleDbDataAdapter(sqlsearch, connection)
' Shows the records and updates the DataGridView
Dim schomasys As New DataTable("PersonalDetails")
dataAdapter.Fill(schomasys)
dgrid.DataSource = schomasys
Catch ex As Exception
MsgBox(ErrorToString)
Finally
connection.Close()
End Try
ElseIf radedubtn.Checked = True Then
' SQL Statement to search for either Name or ID Number
Dim sqlsearch As String
sqlsearch = "SELECT * FROM EducationRecords"
Try
If Me.combosearch.Text = "Name" Then
sqlsearch = sqlsearch & " where sname like '%" & Me.txtsearch.Text & "%'"
Else
sqlsearch = sqlsearch & " where idno =" & Me.txtsearch.Text
End If
' Once again we execute the SQL statements against our DataBase
Dim adapter As New OleDbDataAdapter(sqlsearch, connection)
' Shows the records and updates the DataGridView
Dim schomasys As New DataTable("EducationRecords")
dataAdapter.Fill(schomasys)
dgrid.DataSource = schomasys
Catch ex As Exception
MsgBox(ErrorToString)
Finally
connection.Close()
End Try
ElseIf radpbtn.Checked = True Then
' SQL Statement to search for either Name or ID Number
Dim sqlsearch As String
sqlsearch = "SELECT * FROM payment"
Try
If Me.combosearch.Text = "Name" Then
sqlsearch = sqlsearch & " where sname like '%" & Me.txtsearch.Text & "%'"
Else
sqlsearch = sqlsearch & " where idno =" & Me.txtsearch.Text
End If
' Once again we execute the SQL statements against our DataBase
Dim adapter As New OleDbDataAdapter(sqlsearch, connection)
' Shows the records and updates the DataGridView
Dim schomasys As New DataTable("Payment")
dataAdapter.Fill(schomasys)
dgrid.DataSource = schomasys
Catch ex As Exception
MsgBox(ErrorToString)
Finally
connection.Close()
End Try
Else
MsgBox("Record not found!")
End If
End Sub