I am having a problem wit searching my database when i click search it says syntax error with FROM Clause. Here is my code n screen shot! Its the code for the find button!!
Option Explicit On
Public Class Form1
Private Sub DisplaySearchResults(ByVal objectname As String, ByVal FieldName As String, ByVal Searchdata As String)
Dim SQL_String As String
dbConnection = New OleDb.OleDbConnection(ConnectString)
dbAdapter = New OleDb.OleDbDataAdapter
dtSearch = New DataTable()
SQL_String = "SELECT * FROM " & objectname & "WHERE " & FieldName & " LIKE " & "'" & Searchdata & "'" & ""
dbAdapter = New OleDb.OleDbDataAdapter(SQL_String, ConnectString)
dbConnection.Open()
dbAdapter.Fill(dtSearch)
grdSearch.DataSource = dtSearch
dbConnection.Close()
End Sub
Private Sub btnWards_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWards.Click
grdHospital.DataSource = DisplayData("tblWards")
End Sub
Private Sub btnPatients_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatients.Click
grdHospital.DataSource = DisplayData("tblPatients")
End Sub
Private Sub btnPatientsWards_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatientsWards.Click
grdHospital.DataSource = DisplayData("qryPatientsByWard")
End Sub
Private Sub btnWardNameSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWardNameSearch.Click
Dim objectname As String
Dim Fieldname As String
Dim Searchdata As String
objectname = "tblWard"
Fieldname = "WardName"
Searchdata = cboWardName.Text
DisplaySearchResults(objectname, Fieldname, Searchdata)
End Sub
Private Sub btnPatientSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatientSearch.Click
Dim ObjectName As String
Dim FieldName As String
Dim SearchData As String
ObjectName = "tblPatients"
FieldName = "PatientName"
SearchData = txtSearch.Text
DisplaySearchResults(ObjectName, FieldName, SearchData)
End Sub
Private Sub btnPatientAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPatientAdd.Click
Dim SQL_Insert As String
SQL_Insert = "INSERT INTO tblPatients( PatientName, WardID )" & "VALUES ( '" & txtPatientName.Text & "', " & (cboWardName.SelectedIndex + 1) & ") "
ExecuteSQL(SQL_Insert)
grdHospital.DataSource = DisplayData("tblPatients")
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim intPatientID As Integer
If (IsNumeric(txtDelPatient.Text)) Then
intPatientID = CInt(txtDelPatient.Text)
Dim intUpdated As Integer
intUpdated = ExecuteSQL("DELETE FROM tblPatients WHERE PatientID = " & intPatientID)
If intUpdated > 0 Then
grdHospital.DataSource = DisplayData("tblPatients")
MsgBox("Patient Succesfully Deleted", MsgBoxStyle.Information, "Deleted")
Else
MsgBox("Patient not found in Table", MsgBoxStyle.Critical, "Error")
End If
Else
MsgBox("Invalid Patient ID", MsgBoxStyle.Critical, "Error")
End If
End Sub
End Class
Imports System.Data.OleDb
Module Module1
Public dbConnection As OleDbConnection
Public dbCommand As OleDbCommand
Public dbAdapter As OleDbDataAdapter
Public ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = Hospital.mdb"
Public dtHospital As DataTable
Public dtSearch As DataTable
Public Function DisplayData(ByVal selectdata As String) As DataTable
dbConnection = New OleDbConnection(ConnectString)
dbAdapter = New OleDbDataAdapter
dtHospital = New DataTable()
dbCommand = New OleDbCommand(selectdata)
dbCommand.CommandType = CommandType.TableDirect
dbAdapter.SelectCommand = dbCommand
dbAdapter.SelectCommand.Connection = dbConnection
dbConnection.Open()
dbAdapter.Fill(dtHospital)
Return dtHospital
dbConnection.Close()
End Function
Public Function ExecuteSQL(ByVal SQL_String As String) As Integer
Dim intRowsAffected As Integer
Dim dbConnection As New OleDbConnection(ConnectString)
Dim dbCommand As New OleDbCommand
Dim dbAdapter As New OleDbDataAdapter()
Dim dtHospital As New DataTable()
dbCommand.CommandType = CommandType.Text
dbCommand.CommandText = SQL_String
dbAdapter.SelectCommand = dbCommand
dbAdapter.SelectCommand.Connection = dbConnection
dbConnection.Open()
intRowsAffected = dbCommand.ExecuteNonQuery
dbConnection.Close()
Return intRowsAffected
End Function
End Module