[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Count records in a recordset (VBA)

Posted on 2001-09-03
9
Medium Priority
?
695 Views
Last Modified: 2012-09-13
I have a recordset called rsMerge in my code and I want to get the number of records in that recordset so that I can do a test.

If the number of records is 1 the program flow will be different, and if more than 1 then the flow will be different.

I have tried using .MoveLast and .RecordCount but this is extremely slow, and I have 300,000 records to process.

I need something much faster! Please can anyone help?
0
Comment
Question by:naqayya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 12

Expert Comment

by:Paurths
ID: 6450942
hi nagayya,

have u tried to only use rsMerge.Movenext ? (should work)


or else u could use the DCount-function:
var = DCount("SomeField","YourTable","SomeField = Criteria")

hope this helps,
Ricky
0
 

Author Comment

by:naqayya
ID: 6450995
thanks Ricky,

sorry, i don't understand how to use rsMerge.Movenext to count the number of records in rsMerge. Could you please explain?

Thanks.
0
 

Author Comment

by:naqayya
ID: 6451053
Ricky,

the DCount function does not work with recordsets, only tables or queries.

Here is the code i am using:

With rsMerge
                .MoveLast
               
                If .RecordCount = 1 Then
                    CurrentDb.Execute "INSERT INTO tblDonationAllocations_New VALUES (" & !DonationID & ",'" & !DonationType & "','" & !DonationDetails & "'," & !Amount & ",'" & !PayMethod & "','" & !AppealCode & "','" & !OnBehalfOf & "');"
                    CurrentDb.Execute "INSERT INTO tblDonationAllocations_Done VALUES (" & !Serial & ");"
                   
                ElseIf .RecordCount > 1 Then
                    .MoveFirst
                    SumAmount = 0
                    Do While Not .EOF
                        CurrentDb.Execute "INSERT INTO tblDonationAllocations_Done VALUES (" & !Serial & ");"
                        SumAmount = SumAmount + !Amount
                        .MoveNext
                    Loop
                    CurrentDb.Execute "INSERT INTO tblDonationAllocations_New VALUES (" & myDonationID & ",'" & myDonationType & "','" & myDonationDetails & "'," & SumAmount & ",'" & rsAllDonAlloc!PayMethod & "','" & rsAllDonAlloc!AppealCode & "','" & myOnBehalfOf & "');"
                End If
                .Close
            End With


Any more suggestions? Thanks.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 12

Accepted Solution

by:
Paurths earned 400 total points
ID: 6451067
hi ngayya,

yes, the dcount only works with tables and queries.
That is what i meant.

on the other hand, concerning the movenext:
replace :
.MoveLast
by
.MoveNext



With rsMerge
               .MoveNext
               
               If .RecordCount = 1 Then
                   CurrentDb.Execute "INSERT INTO tblDonationAllocations_New VALUES (" & !DonationID
& ",'" & !DonationType & "','" & !DonationDetails & "'," & !Amount & ",'" & !PayMethod & "','" & !AppealCode
& "','" & !OnBehalfOf & "');"
                   CurrentDb.Execute "INSERT INTO tblDonationAllocations_Done VALUES (" & !Serial & 
");"
                   
               ElseIf .RecordCount > 1 Then
                   .MoveFirst
                   SumAmount = 0
                   Do While Not .EOF
                       CurrentDb.Execute "INSERT INTO tblDonationAllocations_Done VALUES (" & !Serial
& ");"
                       SumAmount = SumAmount + !Amount
                       .MoveNext
                   Loop
                   CurrentDb.Execute "INSERT INTO tblDonationAllocations_New VALUES (" & myDonationID
& ",'" & myDonationType & "','" & myDonationDetails & "'," & SumAmount & ",'" & rsAllDonAlloc!PayMethod
& "','" & rsAllDonAlloc!AppealCode & "','" & myOnBehalfOf & "');"
               End If
               .Close
           End With


0
 
LVL 12

Expert Comment

by:Paurths
ID: 6451077
example for the DCount:

assuming the sql-statement for the rsMerge is like this:

set rsMerge= db.openrecordset("Select Field1, Field2 from YourTable where Field3 = 'YourCriteria';")


then u can use the dcount like this:

var = DCount("Field1","YourTable","Field3 = 'YourCriteria'")

var will now hold the value of the amount of records found.
0
 
LVL 9

Expert Comment

by:perove
ID: 6451087
..or you could check if there is on ore more record by
1 Go to first record
2.Go on record forward and siee if EOF=true




Function testing()
Dim rs As Recordset
Dim db As Database

Set db = CurrentDb
Set rs = db.OpenRecordset("table1")

If rs.BOF Then 'check if no records
    MsgBox "no records at all"
    Exit Function
End If

rs.MoveFirst 'go to frist
rs.MoveNext 'go to second
  If rs.EOF Then  'check if one record
     MsgBox "1 records only in this recordset"
     Exit Function

End If
 


End Function
0
 
LVL 54

Expert Comment

by:nico5038
ID: 6451152
Hi all,

Just miss in the Q and comments the "empty" situation or no records at all.
I guess the code for filling the recordset is also like:
set rsMatch = dbcurrent.openrecordset("SELECT ... FROM ... WHERE X=" & intX & ";")

To speed thing up you are better off with defining a query like:
"SELECT ... FROM ... WHERE X= [parameter];"
This query can be "compiled" by access on forehand and will thus execute faster. You will need to specify the parameter in the code..

perove's .movenext is also faster as a movelast, but when the number can be one, then the total won't be that enormous too, that's why the slow response looks odd.
I guess the .movelast won't differ much as the prime selection will be the most time consuming. Jusrt use the single step mode to execute your code and you get a first impression where the time is consumed....

And check for EOF & BOF to make sure records are there.

Nic;o)
0
 
LVL 12

Expert Comment

by:Paurths
ID: 6451174
yep,

movenext is faster than movelast,
as i commented in the first post

:-)
Ricky
0
 

Author Comment

by:naqayya
ID: 6451340
Solved the problem using .MoveNext and (mainly) setting indexes on the main table.

Thanks.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question