Form not closing

  • Thread starter Afrosheen via AccessMonster.com
  • Start date
A

Afrosheen via AccessMonster.com

Thanks for reading this.
I have a form were if there are no records it is supposed to display a
message stating there are no records then close the form. The problem is that
it does display the message, but it doesn't close the form. The form will
still display but will be blank because of empty records. I've tried docmd.
close,"name of the form". It still displays the form. I've even tried "On
Load" and "On Currant" Am I doing something wrong?
Here the code:

Private Sub Form_Open(Cancel As Integer)
10 On Error GoTo Form_Open_Error

50 If Me.RecordsetClone.RecordCount <> 0 Then
60 Exit Sub
70 Else
80 Call MsgBox("There are no records on file at this time.",
vbInformation, Application.Name)
90 DoCmd.Close
100 End If

110 On Error GoTo 0
120 Exit Sub

Form_Open_Error:
130 Err.Description = Err.Description & " In Procedure " & "Form_Load
of VBA Document Form_frmEmployeeArchive"
140 Call LogError(Err.Number, Err.Description, "Form_Open")

End Sub
 
D

Dirk Goldgar

Afrosheen via AccessMonster.com said:
Thanks for reading this.
I have a form were if there are no records it is supposed to display a
message stating there are no records then close the form. The problem is
that
it does display the message, but it doesn't close the form. The form will
still display but will be blank because of empty records. I've tried
docmd.
close,"name of the form". It still displays the form. I've even tried "On
Load" and "On Currant" Am I doing something wrong?
Here the code:

Private Sub Form_Open(Cancel As Integer)
10 On Error GoTo Form_Open_Error

50 If Me.RecordsetClone.RecordCount <> 0 Then
60 Exit Sub
70 Else
80 Call MsgBox("There are no records on file at this time.",
vbInformation, Application.Name)
90 DoCmd.Close
100 End If

110 On Error GoTo 0
120 Exit Sub

Form_Open_Error:
130 Err.Description = Err.Description & " In Procedure " &
"Form_Load
of VBA Document Form_frmEmployeeArchive"
140 Call LogError(Err.Number, Err.Description, "Form_Open")

End Sub


You need to cancel the Open event, by setting the event procedure's Cancel
argument to True:

If Me.RecordsetClone.RecordCount = 0 Then

MsgBox _
"There are no records on file at this time.", _
vbInformation, _
Application.Name

Cancel = True

End If

Note that if the form was opened by VBA code, cancelling its Open event will
raise error 2501 ("action cancelled") in the code procedure that opened the
form. You would want to trap and ignore that error in that procedure's
error-handling code.
 
J

JimBurke via AccessMonster.com

I think the problem may be that since there are no records, the form goes to
a new record. It's probably not letting you close the form because you're in
the middle of adding a new row. I have no idea how you have your form and
properties, etc., set up, but I believe that if a form is set up to be able
to add new records and the form's control source has no records it will
automatically go to a new record when opened. What I would do in a case like
this is check for whether there are any records before attempting to open the
form - maybe do a dcount on the query or table or whatever it is the form is
bound to.
 
A

Afrosheen via AccessMonster.com

Thanks Dirk, I did get it working. I had to add a openform command after the
cancel because it was closing the database.

Thanks.
I think the problem may be that since there are no records, the form goes to
a new record. It's probably not letting you close the form because you're in
the middle of adding a new row. I have no idea how you have your form and
properties, etc., set up, but I believe that if a form is set up to be able
to add new records and the form's control source has no records it will
automatically go to a new record when opened. What I would do in a case like
this is check for whether there are any records before attempting to open the
form - maybe do a dcount on the query or table or whatever it is the form is
bound to.
Thanks for reading this.
I have a form were if there are no records it is supposed to display a
[quoted text clipped - 25 lines]
 

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