prevent closing a form and go to subform if no records in subform???

T

tlyczko

I have been looking on the NGs and I found this code to show if a
subform has no records.

I have this code in the MAIN form OnClose event:

Private Sub btnClose_Click()
'4/16/06 new code that cancels the close event if the BeforeUpdate code
fires
'4/16/06 see
http://groups.google.com/group/comp...es*+author:tina&rnum=1&hl=en#54bd02708d914487
'DoCmd.RunCommand acCmdCloseWindow
On Error GoTo Err_Handle

'4/16/06 try to ensure that a record is entered in subform, doesn't
work yet
If Me.fsubAddNewRecordReviews.Form.RecordsetClone.RecordCount = 0 Then
MsgBox "no records in subform"
End If

'4/16/06 ensure that BeforeUpdate validation code runs before closing
form
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close , , acSaveNo

Err_Exit:
Exit Sub

Err_Handle:
Select Case Err.Number
Case Not 2501
MsgBox Err.Number & " " & Err.Description, "Unknown error"
End Select
End Sub

What I *want* to do is either here or in the MAIN form BeforeUpdate
event or in SOME event, ensure that the SUBFORM has at least one record
created, I don't want the main form to be closed without telling the
end user to fill in the fields in the SUBFORM.

So far all I have found is the above way to know that the main form has
no records.

Both the main form and subform are bound forms, this is not going to
change.

All I need is to prevent the main form from closing if the subform has
no records.

Thank you, Tom
 
S

salad

tlyczko said:
I have been looking on the NGs and I found this code to show if a
subform has no records.

I have this code in the MAIN form OnClose event:

Private Sub btnClose_Click()
'4/16/06 new code that cancels the close event if the BeforeUpdate code
fires
'4/16/06 see
http://groups.google.com/group/comp...es*+author:tina&rnum=1&hl=en#54bd02708d914487
'DoCmd.RunCommand acCmdCloseWindow
On Error GoTo Err_Handle

'4/16/06 try to ensure that a record is entered in subform, doesn't
work yet
If Me.fsubAddNewRecordReviews.Form.RecordsetClone.RecordCount = 0 Then
MsgBox "no records in subform"
End If

'4/16/06 ensure that BeforeUpdate validation code runs before closing
form
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close , , acSaveNo

Err_Exit:
Exit Sub

Err_Handle:
Select Case Err.Number
Case Not 2501
MsgBox Err.Number & " " & Err.Description, "Unknown error"
End Select
End Sub

What I *want* to do is either here or in the MAIN form BeforeUpdate
event or in SOME event, ensure that the SUBFORM has at least one record
created, I don't want the main form to be closed without telling the
end user to fill in the fields in the SUBFORM.

So far all I have found is the above way to know that the main form has
no records.

Both the main form and subform are bound forms, this is not going to
change.

All I need is to prevent the main form from closing if the subform has
no records.

Thank you, Tom
Maybe use the OnUnload event. Ask the user if they want to continue.
If so, delete their current record. If not, cancel = True.
 
T

tlyczko

I think I tried that already...I will try again, though.

However I do not want to delete the current record from the main form.

I want to ensure that the end user ADDS a record to the SUBform.

Thank you, Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top