Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Developer Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
GetDataCount Function
Kevin Robertson 
          
2 years ago
I wrote a function to display a generic message if there are no records in the table, otherwise open the Form or Report (see screenshot).

Richard I am posting this here in case you wanted to make a Developer Tech Help from it.
Kevin Robertson OP  @Reply  
          
2 years ago

Richard Rost  @Reply  
          
2 years ago
Looks good. For a report you can also use No Data.
Kevin Robertson OP  @Reply  
          
2 years ago
Yeah. This saves having to use On Error Resume Next when the open command is cancelled.
You could, of course, set up custom messages depending on the value sent to the function.
Richard Rost  @Reply  
          
2 years ago
Of course, if your only goal is to see whether or not there are any records, then you should use DLookup. It has much less overhead than DCount or DSum because those functions have to go thru all of the records in the table. Whereas, with DLookup, as soon as it gives you one ID, it's done. So, if your goal is to see if there are any records, just use DLookup. Faster. :)
Richard Rost  @Reply  
          
2 years ago
Maybe...

DetailsFunction AreThereRecords(DataSource As String) As Boolean
    Dim ID As Variant
    ID = DLookup("*", DataSource)
    AreThereRecords = Not IsNull(ID)
End Function


Haven't tested this yet... but trying it now.
Richard Rost  @Reply  
          
2 years ago
Blast... can't use * with Access. I think that's SQL Server only. So you'll have to send the ID...

DetailsFunction AreThereRecords(IDFieldName As String, DataSource As String) As Boolean
    
    Dim ID As Long
    
    ID = Nz(DLookup(IDFieldName, DataSource), 0)
    If ID = 0 Then
        AreThereRecords = False
    Else
        AreThereRecords = True
    End If
    
End Function
Richard Rost  @Reply  
          
2 years ago
Works fine... but you've gotta send the ID...

Status "Are there customers: " & AreThereRecords("CustomerID", "CustomerT")

Hmmm.... what if the function could look up the first field name in the table...

Richard Rost  @Reply  
          
2 years ago
Ahhh... now here we go...

DetailsFunction AreThereRecords(DataSource As String) As Boolean

    Dim rs As Recordset, FieldName As String

    Set rs = CurrentDb.OpenRecordset(DataSource)
    FieldName = rs.Fields(0).Name
    AreThereRecords = Not IsNull(DLookup(FieldName, DataSource))
    rs.Close
    Set rs = Nothing
    
End Function


That works like a champ. :)

So yes... this would make a cool video.
Sami Shamma  @Reply  
             
2 years ago
Richard, that last one was very very clever
Richard Rost  @Reply  
          
2 years ago
Yeah, that's definitely going in a video. :)

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/22/2026 9:26:00 PM. PLT: 1s