I have a main form that has a check box. If the check box is checked, the on_click option is to determine if the current record ID exists. 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 option is to delete data from the table where the ID matches the ID on the current record.
Problem one is the insert adds one record into the table with correct data, and one record with just the ID. I discover this by taking off the allow duplicates in the Lost_LKU table ID field. If I leave the no duplicates allowed on the Lost_LKU table ID field, I receive an error saying record or ID already exists. Very confusing. The second problem is the records that are unchecked all show as gray rather than white.
I am new to this VBA stuff so please offer any suggestions that you think might lead me in the right direction. Here is the code I am running in the on_click event of the check box.
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 record
Set db = CurrentDb
DoCmd.RunSQL "INSERT INTO [Lost_LKU](ID, Last, Lost, DateAdded)" _
& "VALUES([ID], [Last], -1, Date());"
DoCmd.Requery
DoCmd.GoToRecord , , acGoTo, curr_rec
Exit Sub
Else
'Code if ID is found - Delete record
strSQL = ("DELETE * FROM Lost_LKU WHERE [ID] = Forms!MasterForm!ID;")
DoCmd.RunSQL strSQL
DoCmd.Requery
DoCmd.GoToRecord , , acGoTo, curr_rec
End If
Exit Sub
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
Problem one is the insert adds one record into the table with correct data, and one record with just the ID. I discover this by taking off the allow duplicates in the Lost_LKU table ID field. If I leave the no duplicates allowed on the Lost_LKU table ID field, I receive an error saying record or ID already exists. Very confusing. The second problem is the records that are unchecked all show as gray rather than white.
I am new to this VBA stuff so please offer any suggestions that you think might lead me in the right direction. Here is the code I am running in the on_click event of the check box.
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 record
Set db = CurrentDb
DoCmd.RunSQL "INSERT INTO [Lost_LKU](ID, Last, Lost, DateAdded)" _
& "VALUES([ID], [Last], -1, Date());"
DoCmd.Requery
DoCmd.GoToRecord , , acGoTo, curr_rec
Exit Sub
Else
'Code if ID is found - Delete record
strSQL = ("DELETE * FROM Lost_LKU WHERE [ID] = Forms!MasterForm!ID;")
DoCmd.RunSQL strSQL
DoCmd.Requery
DoCmd.GoToRecord , , acGoTo, curr_rec
End If
Exit Sub
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub