Record Counts, using linked SQL files in Access.

I'm creating a conversion routine to create a backend SQL Server database from some legacy imported files.  I first wrote and tested the conversion routines using Access tables with a small input set of data.
Once the routines were debugged I linked to the SQL tables instead of Access tables.

Since the conversion runs so long I used the recordcount property of the Access table to show a status on the screen as the conversion progressed.  

My conversion loops all look similar to this:
'
Dim rsOut As ADODB.Recordset
Set rsOut = New ADODB.Recordset
rsOut.Open outputTable, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'
'
Dim rsIn2 As ADODB.Recordset
Set rsIn2 = New ADODB.Recordset
rsIn2.Open inputTable, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'
If rsIn2.EOF Then
    '
Else
'    If rsIn2.RecordCount > 0 Then
            '
         wkTotRecs = rsIn2.RecordCount
        '
        rsIn2.MoveFirst
        While Not rsIn2.EOF
            '
            wkRecCnt = wkRecCnt + 1
            '
            rsOut.AddNew
            '
        '    rsOut![ID] = Nz(rsIn2![ID], 0)    ' let this one assign it's own ID to link to the detail records
            '
            '
            rsOut.Update
            '
            rsIn2.MoveNext
        Wend
            
'    End If  'recordcount > 1
End If
'
rsIn2.Close
Set rsIn2 = Nothing
'
rsOut.Close
Set rsOut = Nothing

Open in new window


The same loops that worked against the Access tables didn't work against the SQL tables.  As I deguugged I found that the recordcount property always seems to be set to -1, regardless of how many records are in the recordset.

I had to change my loops not to be based on the recordcount being greater than 0.  That was easy enough.

My question is:
Since the recordcount property doesn't seem to be supported: How to get the number of returned records from a SQL recordset?
LVL 1
mlcktmguyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
> How to get the number of returned records from a SQL recordset?
@@ROW_COUNT will always return the number of rows affected from the previous T-SQL statement.
SELECT blah, blah, blah
FROM YourTable

SELECT @@ROW_COUNT  -- < --  Looky here

Open in new window

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Do a:

Select Count(*) from <recordsource>

To get a count of the records before you start your loop.  Getting an accurate record count on a ADO recordset is not always  possible and depends on the datasource and the type of cursor you use.

Jim.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
The -1 by the way indicates that the number of records cannot be determined or the provider does not support the property.

Jim.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

mlcktmguyAuthor Commented:
Thanks, excellent feedback.  I guess I'll just do without the recordcount.  

I'm not executing any T-SQL here, just VBA and I don't want to add any I/O getting the Count(*) to an already lengthy process.

I also now understand the -1.

Thanks
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Try the count; you might be surprised<g>.

JET will push that server side even though not a pass-through query, so it should be very fast.

As an alternative, if processing the records by some sequence, say date, then consider messaging the user with those field(s) every XX rerecords so they can see progress and also run a continuous progress bar.

That way you don't need the exact record count, but still can keep the user informed.

Jim.
mlcktmguyAuthor Commented:
Thanks for the follow up:  
I just created a VBA function that can be used for any table.

Public Function getNumTableRecords(passedTable As String) As Long
'
getNumTableRecords = 0
selectString = " Select Count(*) as NumRecs from " & passedTable
'
Dim rsCount As ADODB.Recordset
Set rsCount = New ADODB.Recordset
rsCount.Open selectString, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'
If rsCount.EOF Then
    '
Else
     '
    getNumTableRecords = rsCount![NumRecs]
End If
'
rsCount.Close
Set rsCount = Nothing

End Function

Open in new window


Doesn't take long at all to execute.

Since I'm pretty new to SQL Server backend I will make a stored procedure out of it, just for practice.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.