Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS SQL Server Express access via VBS

Status
Not open for further replies.

gustaf111

Programmer
Jan 16, 2011
87
SE
Hello,

I have serached the internet how to access how many row there are in a table in a MS SQL database.

Does anybody have an example how to get the number of rows in a table ?

The code and table look like this:
String Value1
Test1 1
Test1 2
Test1 3
Test2 10
Test2 20
Test2 30

I run the SQL command:
SELECT Value1, FROM TestTable WHERE String='Test1'

And after this these commands:
RecordNum = objRecordset.RecordCount
FieldsNum = objRecordset.Fields.count

Value = objRecordset.Fields(0).Value

But the RecordCount returns -1 ? I can se that I that Value is set to 1 which is the first Test1 value. But why does the RecordCount returns -1 and not 3 ?

Cheers Gustaf






 
There seems to be understood that questions on mssql using vbscript should more often than not be asked in vbscript forum. (It is just by usage observed over time.) You can bring the question to vbs forum:

For the present question, I can help with this: When you establish the connection object (say, conn), specify the cursorLocation to client side.
[tt] conn.CursorLocation=3 'adUseClient=3[/tt]
Then you use .RecordCount to get the RecordNum.
 
You can also do

Select count(value1)
FROM TestTable
WHERE String='Test1'

Simi
 
gustaf111 said:
But why does the RecordCount returns -1 and not 3 ?
You usually have to to populate the entire recordset by doing objRecordset.MoveLast before the RecordCount will be accurate.

However, I highly recommend using an SQL statement like the one Simian336 demonstrated, it is much more efficient if all you want is the count of records.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top