Counting records

  • Thread starter Stephen @ ZennHAUS
  • Start date
S

Stephen @ ZennHAUS

Hi Guys and Gals

I wrote a whole lot of code the other day to do this in a test copy of my DB
and deleted the test DB without realizing I hadn't copied the code yet. YAY
for me.

Anyway, I have a form for editing records for a particular table. When the
form loads, it is supposed to run some code that changes the layout of the
form depending on the content of the record to be displayed (it also does
this for Form_Dirty and Form_Current so that as the data changes the form
can "redesign" itself).

Problem is, the table is currently empty and if a user opens the form the
code halts with an error because there are no records in the table. So, I
want a way to count the records in the table BEFORE I execute the code that
redesigns the form so that if there are no records I can display a message
to tell the user to add records before trying to edit them.

The way I did it before was cumbersome. I ran a query (in code, so
connections and recordsets etc.) on the table to count the records in the ID
field. then if the result of the first field in the first row of the records
set =0, I displayed the message then closed the form, otherwise I ran the
code to redesign the form layout.

Any ideas on a quicker and easier method of counting records in recordset of
a form from Form_Load. I have about 20 forms I need to do this to once I get
it working again.

Cheers

Stephen @ ZennHAUS
 
C

Clifford Bass

Hi Stephen,

Just check the NewRecord property since a form opens to a new record
only when there are not records. That is, unless you are opening in an
add-only mode.

If Me.NewRecord Then
' No records

Else
' One or more records

End If

Clifford Bass
 
B

Beetle

Private Sub Form_Load ()

If Me.RecordsetClone.RecordCount <> 0 Then
'run your code
Else
MsgBox "There are no records"
End If

End Sub
 

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

Similar Threads


Top