When I try to edit a record a second time, directly after updating it from the form/subform combo, I get the run-time error '-2147352567 (80020009)
Code:
Option Compare Database
Private Sub cmdAdd_Click()
'when we click add button there are tw option
'1. ADD
'2. Update
If Me.txtID.Tag & "" = "" Then
'this is for inserting new
' add data to tabel
CurrentDb.Execute " INSERT INTO student(StudentID,StudentName,Gender,Phone,Address)" & _
" VALUES (" & Me.txtID & ",'" & Me.txtName & "','" & Me.cboGender & "','" & Me.txtPhone & "','" & _
Me.txtAddress & "')"
Else
'otherwise tag of textid stores the id to be modified
CurrentDb.Execute "UPDATE student " & _
" SET StudentID = " & Me.txtID & _
", StudentName = '" & Me.txtName & "'" & _
", Gender = '" & Me.cboGender & "'" & _
", Phone = '" & Me.txtPhone & "'" & _
", Address = '" & Me.txtAddress & "'" & _
" WHERE StudentID = " & Me.txtID.Tag
End If
'clear form
cmdClear_Click
'refresh data in the sub form
formStudentSub.Form.Requery
End Sub
Private Sub cmdClear_Click()
Me.txtID = ""
Me.txtName = ""
Me.cboGender = ""
Me.txtPhone = ""
Me.txtAddress = ""
'focus on ID text box
Me.txtID.SetFocus
'enable edit button
Me.cmdEdit.Enabled = True
'change update button caption to add
Me.cmdAdd.Caption = "Add"
'clear ID tag for reset new
Me.txtID.Tag = ""
End Sub
Private Sub cmdClose_Click()
DoCmd.Close
End Sub
Private Sub cmdDelete_Click()
' delete record
' check existing record
If Not (Me.formStudentSub.Form.Recordset.EOF And Me.formStudentSub.Form.Recordset.BOF) Then
'confirm delete
If MsgBox("Confirm Delete", vbYesNo) = vbYes Then
'delete now
CurrentDb.Execute " DELETE from student " & _
" WHERE StudentID= " & Me.formStudentSub.Form.Recordset.Fields("StudentID")
'refresh data in the sub form
Me.formStudentSub.Form.Requery
End If
End If
End Sub
Private Sub cmdEdit_Click()
'check whether data exist in the list
If Not (Me.formStudentSub.Form.Recordset.EOF And Me.formStudentSub.Form.Recordset.BOF) Then
'get data into text box
With Me.formStudentSub.Form.Recordset
Me.txtID = .Fields("StudentID")
Me.txtName = .Fields("StudentName")
Me.cboGender = .Fields("Gender")
Me.txtPhone = .Fields("Phone")
Me.txtAddress = .Fields("Address")
'store id of student in tag of student id incase of od change
Me.txtID.Tag = .Fields("StudentID")
'change caption of add button to update
Me.cmdAdd.Caption = "Update"
'disable edit button
Me.cmdEdit.Enabled = False
End With
End If
End Sub