strFileName = "C:\Jay\Sample.xls"
strQuery = "SELECT Col1,Col2 FROM SheetName$ WHERE Col1 = 1 AND Col2 = 2"
Set oCurRowData = GetContentFromDB (strFileName, strQuery)
If Not oCurRowData.EOF Then
iRecordCnt = oCurRowData.RecordCount
Data = oCurRowData.Fields(i).Value
End If
'DB Connection with Excel Driver
Function GetContentFromDB(strFileName, strSQLStatement)
Dim objCon, objRecordSet
Set objCon = CreateObject("ADODB.Connection")
objCon.Open "DRIVER={Microsoft Excel Driver (*.xls)};DBQ="&strFileName & ";Readonly=True"
If Err.Number <> 0 Then
Reporter.ReportEvent micFail,"Create Connection", "[Connection] Error has occured. Error : " & Err.Description
Set GetContentFromDB = Nothing
Exit Function
End If
Set objRecordSet = CreateObject("ADODB.Recordset")
objRecordSet.CursorLocation=3
objRecordSet.Open strSQLStatement, objCon,1, 3
Set GetContentFromDB = objRecordSet
If Err.Number <>0 Then
Reporter.ReportEvent micFail,"Open Recordset", "Error has occured.Error Code : " & Err.Description
Set GetContentFromDB = Nothing
Exit Function
End If
Set objRecordSet.ActiveConnection = Nothing
objCon.Close
Set objCon = Nothing
End Function
Comments