3

I am new to Vbscript (3 days to be precise),

I am connecting my script to SQL Server and playing around with databases. I have connected successfully

Now here's what I want to do:

I want to store the value of SELECT COUNT(*) FROM TABLE_A in a variable to be used in VBscript.

This SQL query returns the number of rows in table and it is an integer, but how do I return it and save in a variable?

I tried this:

Dim VARX
SET VARX = connection.execute("SELECT COUNT(*) FROM TABLE_A")

So now VARX should contain the number of rows of TABLE_A.

But this is a wrong way I know. And of course It posts an error "TYPE Mismatch:". Please guide me!

2 Answers 2

3

Try something like this :

Dim rs, varx
SET rs = connection.execute("SELECT COUNT(*) FROM TABLE_A")
varx = rs(0).value

Not my field of expertise actually, here are some references :

Sign up to request clarification or add additional context in comments.

3 Comments

The second Set is wrong. The value returned is a simple number, not an object.
@Anantvaibhav you're welcome. BTW, why unmarked this answer? (is it the updated code didn't work for you?)
@har07, As I said, It worked with some modifications.
0

Here is the final code which worked:

...
Set Recordset=CreateObject("ADODB.Recordset")
ConnString="DRIVER={SQL Server};SERVER=PCX\SQLEXPRESS;UID=sa;PWD=password;DATABASE=testdb"

Dim SQL_Rows, NoOfRows

SQL_Rows = "SELECT Count (*) from Table_A"
Recordset.open SQL_Rows,ConnString
NoOfRows = Recordset(0).value
Recordset.close

MsgBox NoOfRows

Now Message Box shows the number of rows in table_A

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.