On the main form, I have a sub-form that I created using the Design tools and wizard. How can I check if it is linked?
Open the main form in design view. Click on the sub-form control - not the sub-form itself. Open the properties window. You should see "Master field" and "Child field" text boxes. If they are blank, the main form and sub-form are not linked.
-----------------
I don't use "RunCommand acCmdSaveRecord". (I think it should actually be DoCmd.RunCommand acCmdSaveRecord)
To force a save, I use
Code:
If Me.Dirty Then
Me.Dirty = False
End If
---------------------
I'm not sure if the main form is in continuous forms or single form view; or where "Me.LID" is located.
If the main form is in "continuous form" and LID is visible, then selecting a record and clicking the Delete (Cancel??) button will delete a record.
I just don't understand your setup. But... the way I set up the main form, the code will delete the selected record (if LID is on the main form).
If LID is on the sub-form, the reference to LID must be changed to look at the sub-form.
I modified your code a little, so try this on a test dB. (Remember, LID must be on the main form.....)
Code:
Private Sub Cancel_Click()
On Error GoTo Err_Handler
Const MESSAGETEXT = "Confirm deletion of current " & _
"record and any related records?"
Dim strSQL As String
Dim strCriteria As String
Dim Response
strCriteria = "LID = " & Nz(Me.LID, 0)
strSQL = "DELETE * FROM tblMainDB WHERE " & strCriteria
Response = MsgBox(MESSAGETEXT, vbQuestion + vbOKCancel, "Confirm")
If Response = vbOK Then
' has record been saved to table? If so delete it,
' otherwise undo current record
If Not IsNull(DLookup("LID", "tblMainDB", strCriteria)) Then
CurrentDb.Execute strSQL, dbFailOnError
MsgBox "Deleted"
Else
Me.Undo
MsgBox "No records to delete"
End If
DoCmd.Close acForm, Me.Name
Else
MsgBox "You chose Cancel"
End If
Exit_Here:
Exit Sub
Err_Handler:
MsgBox Err.Description, vbExclamation, "Error"
Resume Exit_Here
End Sub