In following code, I can get the count from excel using
objTempRecordset.Fields.Item(0).Value
However, I want to use column name alias in SQL. i.e.
sSQL = "Select Count(*) AS RecCount FROM [NELimits$] A WHERE A.Type = 'A' AND A.ID = " &Chr(39) & "R001" & Chr(39)
and I want to get the result using:
objTempRecordset.Fields.Item("RecCount").Value
I also tried objTempRecordset.Fields.Item("_Count(*)_").Value but no luck
Can someone please let me know how to use column name alias in this case?
Note: Excel has 2 columns ID: with values such as "R001", "R002" Type: with values such as "A","B","C"
Sample code:
sSQL = "Select Count(*) FROM [NELimits$] A WHERE A.Type = 'A' AND A.ID = " &Chr(39) & "R003" & Chr(39)
Sqlquery = sSQL
sFilePath = "C:\Temp\DataSheet.xlsx"
Dim objTempConnection : Set objTempConnection = CreateObject("ADODB.Connection")
Dim objTempRecordSet : Set objTempRecordSet = CreateObject("ADODB.Recordset")
Dim strPath
'Define constants for objTempRecordset
Const adOpenStatic=3
Const adLockOptimistic=3
Const adLockPessimistic=2
Const adCmdText = &H001
'Open connection
objTempConnection.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="& sFilePath &";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
objTempRecordset.ActiveConnection = objTempConnection
objTempRecordset.CursorType = adOpenStatic
objTempRecordset.LockType = adLockOptimistic
objTempRecordset.Open Sqlquery
If objTempRecordset.EOF Or objTempRecordset.BOF Then
msgbox "no record"
End If
msgbox "Record Count: "&objTempRecordset.RecordCount
msgbox "Value:" & objTempRecordset.Fields.Item(0).Value
objTempRecordset.Fields.Item("RecCount").Valuedidn't work as it should have if you used the first SQL query (not second one inside code block)? In first, you assigned a column alias RecCount but not in second which the ACE SQL engine assigns as the generic Expr1000.