My project has two forms. frmTutor is a form of tutor's profile and there's an 'Update button on it'. Once the button is clicked,it will prompt an input box for the user to key in the name of a tutor. After that, frmUpdateTutor will show and display all the details of the tutor. frmUpdateTutor will have a 'Done' button for the user to click on after editing all of the details of the tutor. In my database, every tutor has its own specific ID. And in this case, I want to get the ID for the tutor in the input box and pass the value to a variable in frmUpdateTutor, so that I can update the tutor profile based on the ID given.
I have searched through Google and this site, but all of the solutions could not solve my problem. So I guess my problem is somewhat different from those faced by the others.
The following is the codes for frmTutor:
Imports System.Data
Imports System.Data.OleDb
Public Class frmTutor
Dim provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
Dim dataFile As String = "VP.accdb"
Dim connString As String = provider & dataFile
Dim myConnection As New OleDbConnection
Dim dr As OleDbDataReader
Dim TemporaryID As Integer = 0
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
myConnection.ConnectionString = connString
myConnection.Open()
Dim TutorName As String = InputBox("Enter Tutor Name :", "Search")
If TutorName = "" Then
MsgBox("Please enter a name!", MsgBoxStyle.Critical, "Error")
myConnection.Close()
Exit Sub
End If
Dim sql As String = "SELECT * FROM tblTutor WHERE TutorName='" & TutorName & "'"
Dim cmd As New OleDbCommand(sql, myConnection)
Dim RecordChecking As String = ""
Dim TemporaryID As Integer = 0
dr = cmd.ExecuteReader
While dr.Read()
RecordChecking = dr("TutorName").ToString
TemporaryID = dr("ID").ToInteger
frmUpdateTutor.txtName.Text = dr("TutorName").ToString
frmUpdateTutor.txtNRIC.Text = dr("NRIC").ToString
If dr("Gender").ToString = "Male" Then
frmUpdateTutor.rbtnMale.Checked = True
ElseIf dr("Gender").ToString = "Female" Then
frmUpdateTutor.rbtnFemale.Checked = True
End If
frmUpdateTutor.txtLocation.Text = dr("Location").ToString
End While
If RecordChecking = "" Then
MsgBox("Record not found!", MsgBoxStyle.Critical, "Error")
myConnection.Close()
Exit Sub
End If
myConnection.Close()
frmUpdateTutor.TemporaryID = TemporaryID
frmUpdateTutor.ShowDialog()
End Sub
End Class
The following is the codes for frmUpdateTutor:
Imports System.Data
Imports System.Data.OleDb
Public Class frmUpdateTutor
Dim provider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
Dim dataFile As String = "VP.accdb"
Dim connString As String = provider & dataFile
Dim myConnection As New OleDbConnection
Dim dr As OleDbDataReader
Dim TemporaryID As Integer = frmTutor.TemporaryID
Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
myConnection.ConnectionString = connString
myConnection.Open()
Dim Gender As String = ""
If rbtnMale.Checked = True Then
Gender = "Male"
ElseIf rbtnFemale.Checked = True Then
Gender = "Female"
End If
Dim Status As String = ""
If rbtnActive.Checked = True Then
Status = True
ElseIf rbtnInactive.Checked = True Then
Status = False
End If
Dim sql As String = "UPDATE tblTutor " & _
"SET [TutorName]='" & txtName.Text & "'," & _
"[NRIC]='" & txtNRIC.Text & "'," & _
"[Gender]='" & Gender & "'," & _
"[Location]='" & txtLocation.Text & "'," & _
"WHERE ID='" & TemporaryID & "'"
Dim cmd As New OleDbCommand(sql, myConnection)
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
myConnection.Close()
Exit Sub
End Try
If MsgBox("Are you sure you want to update this record?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirmation") = MsgBoxResult.Yes Then
MsgBox("Updated successfully!", MsgBoxStyle.Information, "Message")
myConnection.Close()
frmTutor.Show()
Me.Close()
End If
End Sub
End Class
So sorry for the lengthy code,all help are much appreciated!
Aucun commentaire:
Enregistrer un commentaire