Count records in an SQL string

  • Thread starter Thread starter Ian Chappel
  • Start date Start date
I

Ian Chappel

I have code-generated SQL string, which is to be the RecordSource of a
form.

How do I do a record count before I open the form (I want to avoid opening
if there are no records)?
 
Cancel the Open event of the form if there are no records.

Something like this:
Private Sub Form_Open(Cancel As Integer)
If Me.RecordsetClone.RecordCount = 0 Then
Cancel = True
MsgBox "No records"
End If
End Sub
 
Thanks Allen,

Sorry, I wasn't accurate with my question.

The form's already open. I want to prevent the form's RecordSource being
changed when I click a cmdButton if the new RecordSource would have no
records. So I think I need to count the records as a result of my strSQL
_before_ it becomes the form's RecordSource?
 
If you are building the SQL string dynamically, then you can build the WHERE
clause separately. Then use it in as the Criteria of a DLookup() to see if
any record is returned.

It is possible to respond to the Filter event after the fact, but a
DLookup() sounds like what you are after.

As a worst case, you could assign the SQL string to the SQL property of a
QueryDef you saved for this purpose, and then see if this query generates
any results.
 
Thanks Allen, All sorted now.

Allen Browne said:
If you are building the SQL string dynamically, then you can build the
WHERE clause separately. Then use it in as the Criteria of a DLookup() to
see if any record is returned.

It is possible to respond to the Filter event after the fact, but a
DLookup() sounds like what you are after.

As a worst case, you could assign the SQL string to the SQL property of a
QueryDef you saved for this purpose, and then see if this query generates
any results.
 

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

Access Dcount function in access 0
Counting records 5
Count records from SQL 2
Excel Export Filtered Form Data To Excel 0
filtering a form based on the values of a subform 0
Counting Records 1
Count records if 2
Combo Box - Unique Situation 2

Back
Top