Trapping Blank Form Opening

  • Thread starter Thread starter iholder
  • Start date Start date
I

iholder

I have an form that uses a criteria query. If the criteria query is not meet
the form opens blank. How can I trap this error. So the form does not open
at all.

Thank You
Ileana
 
You can use the Form Open event to check the form's recordset record count.
During the open event, it will return 1 because it has not had a chance to
fully populate it's recordset, but if there are no records, it will return 0.
Here is an example:

Private Sub Form_Open(Cancel As Integer)
If Me.Recordset.RecordCount = 0 Then
MsgBox "No Records"
Cancel = True
End If
End Sub

Setting Cancel = True causes the form to immediately close.
 
I don't understand what you are saying. What error are you getting and on
which line of code. I tested it in 2003 and it worked for me. Can you
provide more detail, please.
 
Sorry, No Error Message. Just "No Record" Message. And form is still opening.
I do not want to open form.
 
Did you put the code in the form's Open event?
Do you have the line Cancel = True in the code?
What version of Access are you running?
 
I copied and paste your code into the Form Open Event. I included Cancel -
true
I am running Access 2002.
 
I don't know why it is not working. I tested it before I posted the answer.
If that doesn't work in 2002, then you can replace the Cancel = True line with
Docmd.Close acForm, Me.Name, acSaveNo
 
Good. This puzzles me.
Even back to AC98, the Form Open event has had the Cancel argument. It's
purpose is to cause the form to close when you cancel the event. That is the
purpose of it and that is why you normally use Cancel = True.

There most be something else going on I don't know about.
 
No puzzle. I test it again with cancel = true and it works. It works when the
form is opened from a command button. And the forms closes out.

But I am experiencing another problem. When the form criteria is meet. Upon
closing the form. I get an error message

"Runitime error - 2147352567 ("800200009'). This recordset is not updateable."

The error code line is
Private Sub LastName_LostFocus()
Me.LastName = StrConv(Me.LastName, vbProperCase)
End Sub

I think unrelated. Your help on this error message would be greatly
appreciated.
 
Are you able to edit any records in the form's recordset?
What the message is saying is the form's recordset can't be updated. That
can be caused when you are using either a Union query or a complex multi
table query that becomes not editable. If you form is based on a query and
can't be updated, you can look at the navigation buttons on the form. If the
button with the * (add record) is disabled, that means you can't add a record
to the recordset. It also means the recordset can't be edited.
 
Back
Top