Private Sub btnSave_Click()
On Error GoTo Err_btnSave_Click
'Create functional variables
Dim strSQL As String
Dim message As String
'Create variables for input fields.
Dim Employee As Integer
Dim FName As String
Dim LName As String
Dim Title As String
Dim Address As String
Dim City As String
Dim Prov As String
Dim PCode As String
Dim Phone As String
Dim WorkEmail As String
If IsNull(cboEmployee.Value) = False Then
'Assign fields to variables
Employee = cboEmployee.Value
FName = txtFName.Value
LName = txtLName.Value
Title = txtTitle.Value
Address = txtAddress.Value
City = txtCity.Value
Prov = cboProv.Value
PCode = txtPostalCode.Value
Phone = txtPhone.Value
WorkEmail = txtWorkEmail.Value
'Open the database
Set db = CurrentDb()
'Open the recordset
Set rs = db.OpenRecordset("Employee")
'SQL statement to delete all records that have the matching Employee ID from the cboEmployee control
strSQL = "DELETE * FROM [Employee] WHERE [EmployeeID] = " & Employee
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
'Fill in the Employee table with the values from the form fields
rs.AddNew
rs.Fields("FName").Value = FName
rs.Fields("LName").Value = LName
rs.Fields("Title").Value = Title
rs.Fields("Address").Value = Address
rs.Fields("City").Value = City
rs.Fields("Prov").Value = Prov
rs.Fields("PostalCode").Value = PCode
rs.Fields("Phone").Value = Phone
rs.Fields("WorkEmail").Value = WorkEmail
rs.Update
' Close and reset db and recordset
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
MsgBox "Employee Added.", vbInformation + vbOKOnly, "Employee Management"
'Refresh the employee drop down menu
Me.cboEmployee.Requery
'Clear fields
txtFName.Value = ""
txtLName.Value = ""
txtTitle.Value = ""
txtAddress.Value = ""
txtPhone.Value = ""
txtCity.Value = ""
txtWorkEmail.Value = ""
txtPostalCode.Value = ""
cboProv.Value = Null
Else
'Assign fields to variables
FName = txtFName.Value
LName = txtLName.Value
Title = txtTitle.Value
Address = txtAddress.Value
City = txtCity.Value
Prov = cboProv.Value
PCode = txtPostalCode.Value
Phone = txtPhone.Value
WorkEmail = txtWorkEmail.Value
'Open the database
Set db = CurrentDb()
'Open the recordset
Set rs = db.OpenRecordset("Employee")
'SQL statement to delete all records that have the matching Employee ID from the cboEmployee control
strSQL = "DELETE * FROM [Employee] WHERE [EmployeeID] = " & Employee
DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
DoCmd.SetWarnings True
'Fill in the Employee table with the values from the form fields
rs.AddNew
rs.Fields("FName").Value = FName
rs.Fields("LName").Value = LName
rs.Fields("Title").Value = Title
rs.Fields("Address").Value = Address
rs.Fields("City").Value = City
rs.Fields("Prov").Value = Prov
rs.Fields("PostalCode").Value = PCode
rs.Fields("Phone").Value = Phone
rs.Fields("WorkEmail").Value = WorkEmail
rs.Update
' Close and reset db and recordset
rs.Close
Set rs = Nothing
db.Close
Set db = Nothing
MsgBox "Employee Added.", vbInformation + vbOKOnly, "Employee Management"
'Clear fields
txtFName.Value = ""
txtLName.Value = ""
txtTitle.Value = ""
txtAddress.Value = ""
txtPhone.Value = ""
txtCity.Value = ""
txtWorkEmail.Value = ""
txtPostalCode.Value = ""
cboProv.Value = Null
'Refresh the employee drop down menu
Me.cboEmployee.Requery
End If
Exit_btnSave_Click:
Exit Sub
Err_btnSave_Click:
MsgBox Err.Description
Resume Exit_btnSave_Click
End Sub
|