Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open/close forms without records 1

Status
Not open for further replies.

wsuess

Programmer
Aug 13, 2002
9
0
0
DE
my problem:
one of my forms based on a query, always started, when i login in my database. dependent on the actual date or time of the day the query get no results, so the forms contains nothing and then i get an error mesage.

what i need is a if-instruction or something else, so that the form will be automatically closed, if there are no results or records in the query. but i have no idea ...
 
On the OnOpen event of the form put something like this:

Private Sub Form_Open(Cancel As Integer)
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
On Error Resume Next
rs.MoveLast
On Error GoTo 0
If rs.RecordCount = 0 Then Cancel = True
rs.Close
Set rs = Nothing
End Sub

This counts how many records are in the forms recordset then stops opening the form if none exist.
This code requires a reference setting to the dao library. Go to tools->references and choose Microsoft Data Access Objects 3.6 (or 3.5 if you are using A97)

HTH

Ben ----------------------------------
Ben O'Hara
Home: bpo@RobotParade.co.uk
Work: bo104@westyorkshire.pnn.police.uk
Web: ----------------------------------
 
special thanks to u

it works - have a nice day
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top