I have a main form that has a check box. If the check box is checked, the on_click event is to determine if the current record ID exists in the Lost_LKU table. If the current record ID does not exist, then insert data into a Lost_LKU table. If the check box is unchecked, the on_click event is to delete data from the Lost_LKU table where the ID matches the ID on the current record.
PROBLEM
If I put a check in the box (add data to the Lost_LKU table) I receive an error. When I check the actual table the record is there.
Error Received:
Error No 3022 Description: The change you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.
If I remove the check (delete data from the Lost_LKU table), I receive this error.
Error No 3129 Invalid SQL statement expected ‘Delete’, ‘Insert’, ‘Procedure’, ‘Select’, or ‘Update’.
Here is the code I am running in the on_click event of the check box. Any and all help for this newbee is appriciated.
PROBLEM
If I put a check in the box (add data to the Lost_LKU table) I receive an error. When I check the actual table the record is there.
Error Received:
Error No 3022 Description: The change you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.
If I remove the check (delete data from the Lost_LKU table), I receive this error.
Error No 3129 Invalid SQL statement expected ‘Delete’, ‘Insert’, ‘Procedure’, ‘Select’, or ‘Update’.
Here is the code I am running in the on_click event of the check box. Any and all help for this newbee is appriciated.
Code:
Private Sub Check158_Click()
On Error GoTo ErrorHandler
Dim db As Database
Dim strSQL As String
Dim curr_rec As String
Dim MyDate As String
MyDate = Date
'Sets the current record so that after DoCmd.Requery the same record appears
curr_rec = Me.CurrentRecord
If (IsNull(DLookup("[ID]", "Lost_LKU", "[ID] = " & Me!ID & ""))) Then
' Code if ID is not found - Insert recor
Set db = CurrentDb
DoCmd.RunSQL "INSERT INTO [Lost_LKU](ID, Last, Lost, DateAdded)" & _
"VALUES(" & Me.ID & ", '" & Me.LAST & "', -1, #" & Date & #);"
DoCmd.Requery
DoCmd.GoToRecord , , acGoTo, curr_rec
Exit Sub
Else
'Code if ID is found - Delete record
DoCmd.RunSQL strSQL
DoCmd.Requery
DoCmd.GoToRecord , , acGoTo, curr_rec
strSQL = ("DELETE * FROM Lost_LKU WHERE [ID] = " & Forms!MasterForm!ID & ";")
End If
Exit Sub
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub