Friday, April 9, 2010

VBScript - Use SQL Commands to Query an Excel Workbook

VBScript - Use SQL Commands to Query an Excel Workbook

'* Script name: AdoExcelCount.vbs
'* Created on: 8:08 PM 10/17/2006
'* Author: Kent Finkle
'* Purpose: Use SQL Count to query an Excel workbook.

Option Explicit

Const adOpenStatic = 3
Dim objConnection
Dim objRecordset

Set objConnection = CreateObject("ADODB.Connection")

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\scripts\Book1.xls;" & _
"Extended Properties=""Excel 8.0;"""

Set objRecordset = CreateObject("ADODB.Recordset")

objRecordset.Open "Select Count(*) As RowCount from [Sheet1$]", objConnection, adOpenStatic

Wscript.Echo objRecordset.Fields("RowCount").Value
objRecordset.Close
objConnection.Close

[Via]