473,666 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fastest way to determine if any records exist in table?

adm
There are a few ways to go about this, but what is the fastest when
called from VBA? This if for an ADP with a SQL Server back-end.

Nov 13 '05 #1
7 12093
In Jet and DAO the only time BOF and EOF are both true is when there are no
rows in a result. In ADO you only need to check for EOF. If you ran
something like "SELECT TABLE1.* FROM TABLE1;" in a recordset and checked for
EOF in VBA then it's likely there is nothing in the table.

--
Alan Webb
kn*******@SPAMh otmail.com
"It's not IT, it's IS"

"adm" <ad*****@yahoo. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
There are a few ways to go about this, but what is the fastest when
called from VBA? This if for an ADP with a SQL Server back-end.

Nov 13 '05 #2
adm wrote:
There are a few ways to go about this, but what is the fastest when
called from VBA? This if for an ADP with a SQL Server back-end.


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Run a query that gets the count of the table:

SELECT Count(*) As Recs FROM table

or use DCount():

Debug.Print DCount("*","tab le_name")

--
MGFoster:::mgf0 0 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQlxGgYechKq OuFEgEQIV9ACeMz b4Hi6VGe2mKqPhW EoX8BC6nZoAniil
HYlrT29063JxK81 jAJboGRZn
=QX5w
-----END PGP SIGNATURE-----
Nov 13 '05 #3
"adm" <ad*****@yahoo. com> wrote in news:1113335567 .986886.304740
@o13g2000cwo.go oglegroups.com:
There are a few ways to go about this, but what is the fastest when
called from VBA? This if for an ADP with a SQL Server back-end.


I think this depends to some extent on how frequently there will be some
records (and how many), as the answer to a small extent depends on how many
records.

If the table is populeted:

If the table has a primary key then I think the fastest way to count the
records in it (ADP-MS-SQL) may be:

n = CurrentProject. Connection.Exec ute("SELECT rows FROM sysindexes WHERE
name = 'NameOfPrimaryK ey'").Collect(0 )

This may be as many as 2 milliseconds faster than:

n = CurrentProject. Connection.Exec ute("SELECT Count(*) FROM
TableName").Col lect(0)

n=DCount("*", "TableName" ) seems to be nine times slower than the
previously mentioned methods.

On a remote MS-SQL DB typical tick times were:

Primary Key: 4700
Count: 4750
DCount: 41000

for 50 iterations.
for a table with 594 records.

BUT ...

If the table has no records then Primary Key speed stays the same, while
Count and DCount speeds are reduced by about 10%.

....

so if the table had a primary key and might at times be large I'd use the
primary key rows methods; if it were a small table which was likely to be
empty most of the time or didn't have a primary key (ewwwwwwwwwwwww ww
yuck!) I'd use Count.

--
Lyle
--
From ADO28.chm
Nov 13 '05 #4

Why would the following code not be adequate:

dim db as DAO.Database
dim rst as DAO.Recordset
set db = currentdb()

set rst = db.OpenRecordse t("tablename" , dbopentable)

if rst.recordcount = 0 then
do what is needed
end if

On Wed, 13 Apr 2005 01:23:28 GMT, Lyle Fairfield <Lo******@FFDBA .Com>
wrote:
"adm" <ad*****@yahoo. com> wrote in news:1113335567 .986886.304740
@o13g2000cwo.g ooglegroups.com :
There are a few ways to go about this, but what is the fastest when
called from VBA? This if for an ADP with a SQL Server back-end.


I think this depends to some extent on how frequently there will be some
records (and how many), as the answer to a small extent depends on how many
records.

If the table is populeted:

If the table has a primary key then I think the fastest way to count the
records in it (ADP-MS-SQL) may be:

n = CurrentProject. Connection.Exec ute("SELECT rows FROM sysindexes WHERE
name = 'NameOfPrimaryK ey'").Collect(0 )

This may be as many as 2 milliseconds faster than:

n = CurrentProject. Connection.Exec ute("SELECT Count(*) FROM
TableName").Co llect(0)

n=DCount("*" , "TableName" ) seems to be nine times slower than the
previously mentioned methods.

On a remote MS-SQL DB typical tick times were:

Primary Key: 4700
Count: 4750
DCount: 41000

for 50 iterations.
for a table with 594 records.

BUT ...

If the table has no records then Primary Key speed stays the same, while
Count and DCount speeds are reduced by about 10%.

...

so if the table had a primary key and might at times be large I'd use the
primary key rows methods; if it were a small table which was likely to be
empty most of the time or didn't have a primary key (ewwwwwwwwwwwww ww
yuck!) I'd use Count.

--
Lyle


Nov 13 '05 #5
Lauren Wilson wrote:
Why would the following code not be adequate:

dim db as DAO.Database
dim rst as DAO.Recordset
set db = currentdb()

set rst = db.OpenRecordse t("tablename" , dbopentable)

if rst.recordcount = 0 then
do what is needed
end if


Perhaps, you could try it and report back on its speed, as my test has
not yet completed running? The original poster asked for, I believe, the
fastest way in an ADP/MS-SQL Server application.

Nov 13 '05 #6
adm
great reply, lyle. i was wondering about stuff like "select top 1
from..."(not sure of syntax here)...i wonder if that's faster than
count(*).

Nov 13 '05 #7
adm wrote:
great reply, lyle. i was wondering about stuff like "select top 1
from..."(not sure of syntax here)...i wonder if that's faster than
count(*).


I tried TOP 1 but found it about 7% slower than PK and Count.

--
--
Lyle
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2501
by: Dave | last post by:
I have a form which is based on a table (a) with over 6000 records.I have a button on this form which will open another form with related data from another table (b). What I want to do is open the second form and see the related data or if it hasnt got any related data create a new record in table (b) so that I can input data. If possible I dont want to create a one to one relationship between the tables but create the new records in table...
3
2911
by: Larry Rekow | last post by:
As part of a macro, I'm trying to automate appending a table with new records. let's say the table 2 has some new records in it, but also has a lot of identical records to table 1. I would like to append table 1 with all of the new records, and do it unattended in a macro. Is there a way to specify in the append query to only append the
1
1845
by: KC | last post by:
Hello, I am using Access 2002. WinXP, Template from MS called Orders Mgmt DB. I have tweaked this DB to work for our small co. It has worked pretty well up until I made the mistake of deleting about 80 records from the Orders table. 80 out of a 1000 records. Now our data entry form shows our customer addresses, but not customer order history. When looking at all of the tables, customer, payments, orders, they still have all of the...
7
3672
by: Ryan | last post by:
OK, here's my setup. I have a treeview control that is populated with records from a Product table, and it allows "checking". This is used for my automatic notification system. Say a particular Product is sold, any users who have that boxed checked will get a notification. So my (shortened) database layout is as such: Product Table ProductID (PK) ProductName
2
1643
by: shalini0702 | last post by:
Hi, I am facing problem of Missing Records in Table after Compacting and Repairing. Suppose my table had 486 records before compactig and after compacting I see only 485 records, One records has been automatically deleted can any one tell me what is the problem that is with Msaccess Thanx Shalini
2
3253
by: kevinjbowman | last post by:
I am by no means a SQl Jedi as will be apparent by my question, but I can usually figure out a select statement on my own. I have one today though that really has me stumped. I am working in MySQlL 5. In My first select statement I get all my records from Table B SELECT `table_A`.`ITEM`, `table_A`.`DECSCRIPTION`, `table_A`.`UM`, `table_A`.`PHASE`,
2
7387
by: Lemmuel31 | last post by:
i want to know the code that will determine if the table is existing and if the table exist i will drop it.. Can you give me some source code on that problem?
1
6669
by: Dmitry Kulinich | last post by:
how to select 200 first records from table? -- Thank you, De Cool, EPE
1
1861
by: elbatz | last post by:
Can somebody help me? How do I know if records in Table1 are also in Table2, using VB6 code? For example: If Table1 has 1111 in column1 and also Table2 has 1111 in column2 msgox "Existing record in Table2" else msgbox "No record in Table2"
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.