Data Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Update:

cn.ConnectionString = constr
cn.Open()
Dim sql As String
sql = "Update DataBase set " & _
"[SecondName] = '" & TextBox2.Text.ToString & "',[FirstName] = '" &
TextBox1.Text & "', [LastName] = '" & TextBox3.Text & "' , [Department] ='" &
ComboBox1.Text & "' ,[School]='" & ComboBox2.Text & "', [Gender] = '" & ComboBox3.Text &
"' , [State] ='" & ComboBox4.Text & "' ,[LocalGovt]='" & TextBox5.Text & "' ,[Age] ='" &
TextBox6.Text & "' " & _
"Where MatricNumber = " & TextBox4.Text & ""
Dim command As OleDbCommand = New OleDbCommand(sql, cn)

Try
With command
.Connection = cn
.CommandType = CommandType.Text
.CommandText = sql
End With

command.ExecuteNonQuery()

Catch ex As Exception
MsgBox(ex.Message, vbCritical)
cn.Close()

Finally
cn.Dispose()
cn.Close()
End Try

Submit:
If TextBox4.Text = "" Then
MsgBox("Enter Student Matric Number", MsgBoxStyle.Exclamation)
Exit Sub
End If

cn.ConnectionString = constr

cn.Open()
Dim str As String
str = "Insert Into [DataBase]
([ID],[MatricNumber],[LastName],[FirstName],[SecondName],[Department],[School],[Gender],[
State],[LocalGovt],[Age]) VALUES (?,?,?,?,?,?,?,?,?,?,?) "
Dim cmd As OleDbCommand = New OleDbCommand(str, cn)
Dim id As String = TextBox4.Text.Substring(13)

cmd.Parameters.Add(New OleDbParameter("ID", CType(id, String)))


cmd.Parameters.Add(New OleDbParameter("MatricNumber",
CType(TextBox4.Text.ToString.ToUpper.Trim, String)))
cmd.Parameters.Add(New OleDbParameter("LastName", CType(TextBox3.Text, String)))
cmd.Parameters.Add(New OleDbParameter("FirstName", CType(TextBox1.Text, String)))
cmd.Parameters.Add(New OleDbParameter("SecondName", CType(TextBox2.Text,
String)))
cmd.Parameters.Add(New OleDbParameter("Department", CType(ComboBox1.Text,
String)))
cmd.Parameters.Add(New OleDbParameter("School", CType(ComboBox2.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Gender", CType(ComboBox3.Text, String)))
cmd.Parameters.Add(New OleDbParameter("State", CType(ComboBox4.Text, String)))
cmd.Parameters.Add(New OleDbParameter("LocalGovt", CType(TextBox5.Text, String)))
cmd.Parameters.Add(New OleDbParameter("Age", CType(TextBox6.Text, String)))

Try
cmd.ExecuteNonQuery()
cmd.Dispose()
cn.Close()
MsgBox("Data Successfully Saved")
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
ComboBox1.Text = String.Empty
ComboBox2.Text = String.Empty
ComboBox3.Text = String.Empty
ComboBox4.Text = String.Empty

Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
cn.Close()

Exit Sub
End Try

Retrieve:
Dim matno, n As String
matno = InputBox("Enter Student Matric Number", "Search Student",
"ND2017/1010/002")

Try
cn.ConnectionString = constr
cn.Open()
cm.Connection = cn

n = "SELECT * FROM [DataBase] WHERE(MatricNumber ='" & matno.ToString & "')"


Dim cd As OleDbCommand = New OleDbCommand(n, cn)
dr = cd.ExecuteReader

While dr.Read
found = True
TextBox1.Text = dr("FirstName").ToString
TextBox2.Text = dr("SecondName").ToString
TextBox3.Text = dr("LastName").ToString
TextBox4.Text = dr("MatricNumber").ToString.ToUpper
ComboBox1.Text = dr("Department").ToString
ComboBox2.Text = dr("School").ToString
ComboBox3.Text = dr("Gender").ToString
ComboBox4.Text = dr("State").ToString
TextBox5.Text = dr("LocalGovt").ToString
TextBox8.Text = dr("Age").ToString
End While
cn.Close()
If found = False Then
MsgBox("Student Matric Number not found.", MsgBoxStyle.Critical)
dr.Close()
End If
Catch ex As Exception
MsgBox(ex.Message)
cn.Close()

End Try

Delete:
Dim hos, path, matno, link As String

Dim myconnection As OleDbConnection = New OleDbConnection


hos = "Provider=Microsoft.ACE.OLeDB.12.0;Data Source="
path = "C:\Users\User1.User\Documents\Visual Studio 2010\Projects\AYO
CRUD\StudentsDataBase.accdb"
link = hos & path
myconnection.ConnectionString = link
myconnection.Open()
Dim sql As String
matno = InputBox("Enter Student Matric Number For example 018 NOT
ND2017/1010/002", "Delete Student Records", "018")
sql = "Delete from [DataBase] Where [ID] = " & matno & ""
Dim command As OleDbCommand = New OleDbCommand(sql, myconnection)
Try
command.ExecuteNonQuery()
command.Dispose()
MsgBox("Record Deleted Successfully", MsgBoxStyle.Information)
myconnection.Close()

Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
cn.Close()
End Try

Clear:
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
TextBox6.Clear()
ComboBox1.Text = String.Empty
ComboBox2.Text = String.Empty
ComboBox3.Text = String.Empty
ComboBox4.Text = String.Empty
TextBox1.Focus()

You might also like