Question : Access 2003: Is it possible to edit and update a tables data using a recordset

My code currently loads the table data based on selected combo box value. It displays its value in a unbound form. I can change the data, then click the Save button. Which then opens the RecordSet based on the EmployeeID then it deletes the existing Employee and then saves the Employee, but of course the table thinks this is a new entry so it gives it a new EmployeeID. Is there a way to update an existing employee without having to delete the employee from the table and update its existing values to the new ones supplied on the form? Here is my code for the Save button...

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
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

Answer : Access 2003: Is it possible to edit and update a tables data using a recordset

Random Solutions  
 
programming4us programming4us