Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 24th, 2006, 07:37 AM
Authorized User
 
Join Date: Oct 2006
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default Count Record

Hi Frnds,

          How to count the record in a database and show the result in a messagebox? I want to do it using VBA.

 
Old November 27th, 2006, 07:28 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

Prasanta,

Very simply, to count the records in a table and return the value in a msgbox...

================================================== ===============
Public Function pfunCountRecords(strTableName as string) as Long
dim rst as recordset
dim strMsg as string
dim strTitle as string
dim lngCount as long

strTitle = "Record Count"
set rst = currentdb.openrecordset(strTableName)
lngCount = rst.recordcount

rst.close
set rst = nothing

strMsg = "There are " & lngCount & " record(s) in " & strTableName
msgbox strMsg, vbInformation, strTitle

pfunCountRecords = lngCount

End Function
================================================== ===============

So, as a function this returns the record count value as part of the function, but also pops up a msgbox with the same information.

If it is more than a table you want to evaluate, change the input to a SQL statement instead and evaluate that.

Lee
 
Old November 28th, 2006, 10:24 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

The easiest way to count the number of records in a table in a database is simply to use the DCOUNT function.

MsgBox "The number of records in the table is " & DCount("[lngRecNo]", "tblYourTable"), vbInformation, "Record Count"

Where you replace lngRecNo and tblYourTable with the real names of a non-null field in your table and the actual name of your table. I used these fake names instead.

For a small database, DCOUNT is easier than Lee's answer. For a huge database, Lee's more complex answer will work faster than DCOUNT.


Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
 
Old November 28th, 2006, 10:42 AM
Friend of Wrox
 
Join Date: Sep 2003
Posts: 155
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to leehambly
Default

Bah, I always fall down on D-commands!





Similar Threads
Thread Thread Starter Forum Replies Last Post
record count help jpaul VB Databases Basics 2 January 22nd, 2008 02:49 PM
count no of record sanjeev jha ADO.NET 1 November 5th, 2007 12:28 PM
Record count deb1980 ASP.NET 2.0 Basics 6 April 10th, 2007 12:42 PM
How to count a record when it is not there Mitch Access 1 October 3rd, 2003 03:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.