I figured it out!!!!!!!!! Well, i found a web page that talked about exactly what i was trying to do (here is the web site:
http://www.dbforums.com/microsoft-excel/1633666-sql-vba-excel.html)
Basically i did exactly what they said in the above web site and IT WORKED! Very easy and very cool. My data was stored in the "Results" page of excel. Very nice.
Below is part of my code from my previous post. I am not showing all the code again just the sqlText part which is the most important. The other part of the code seems to work fine.
sqlText = ""
sqlText = sqlText & " DECLARE @T DATETIME"
sqlText = sqlText & " SET @T = '01/12/2009'"
sqlText = sqlText & " ;WITH CTE AS"
sqlText = sqlText & " ("
sqlText = sqlText & " SELECT EmployeeID,"
sqlText = sqlText & " SUM(DateDiff(ss, TimeIn, TimeOut)) As s"
sqlText = sqlText & " FROM EmployeeHours"
sqlText = sqlText & " WHERE TimeIN >= DATEADD(DAY,0, DATEDIFF(DAY,0,@T))"
sqlText = sqlText & " AND TimeIn < DATEADD(DAY,1, DATEDIFF(DAY,0,@T))"
sqlText = sqlText & " GROUP BY EmployeeID"
sqlText = sqlText & " )"
sqlText = sqlText & " SELECT"
sqlText = sqlText & " C.EmployeeID,Emp.FirstName,Emp.LastName,"
sqlText = sqlText & " CAST((s/3600) AS VARCHAR(3)) + ':' +"
sqlText = sqlText & " RIGHT('0' + CAST((s % 3600) / 60 AS VARCHAR(2)), 2) + ':' +"
sqlText = sqlText & " RIGHT('0' + CAST((s % 60) AS VARCHAR(2)), 2) AS 'Hours_worked'"
sqlText = sqlText & " FROM CTE C"
sqlText = sqlText & " JOIN EmployeeList Emp ON C.EmployeeID = Emp.EmployeeID"
I guess i will show all the code just to make sure no one gets confused.
Sub Macro2()
Dim Conn As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim Cmd As New ADODB.Command
Dim sqlText As String
Dim Row As Long
Dim Findex As Long
Dim Data As Worksheet
Dim X As Long
Set Data = Sheets("Results") '<--- Change this to the sheet you want the results on
Data.Select
Cells.ClearContents
Conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=asdf;PWD=asdf1234!;Initial Catalog=Timeclock;Data Source=SAUK\SQLEXPRESS"
Cmd.ActiveConnection = Conn
Cmd.CommandType = adCmdText
sqlText = ""
sqlText = sqlText & " DECLARE @T DATETIME"
sqlText = sqlText & " SET @T = '01/12/2009'"
sqlText = sqlText & " ;WITH CTE AS"
sqlText = sqlText & " ("
sqlText = sqlText & " SELECT EmployeeID,"
sqlText = sqlText & " SUM(DateDiff(ss, TimeIn, TimeOut)) As s"
sqlText = sqlText & " FROM EmployeeHours"
sqlText = sqlText & " WHERE TimeIN >= DATEADD(DAY,0, DATEDIFF(DAY,0,@T))"
sqlText = sqlText & " AND TimeIn < DATEADD(DAY,1, DATEDIFF(DAY,0,@T))"
sqlText = sqlText & " GROUP BY EmployeeID"
sqlText = sqlText & " )"
sqlText = sqlText & " SELECT"
sqlText = sqlText & " C.EmployeeID,Emp.FirstName,Emp.LastName,"
sqlText = sqlText & " CAST((s/3600) AS VARCHAR(3)) + ':' +"
sqlText = sqlText & " RIGHT('0' + CAST((s % 3600) / 60 AS VARCHAR(2)), 2) + ':' +"
sqlText = sqlText & " RIGHT('0' + CAST((s % 60) AS VARCHAR(2)), 2) AS 'Hours_worked'"
sqlText = sqlText & " FROM CTE C"
sqlText = sqlText & " JOIN EmployeeList Emp ON C.EmployeeID = Emp.EmployeeID"
Cmd.CommandText = sqlText
Set RS = Cmd.Execute
For X = 1 To RS.Fields.Count
Data.Cells(1, X) = RS.Fields(X - 1).Name
Next
If RS.RecordCount < Rows.Count Then
Data.Range("A2").CopyFromRecordset RS
Else
Do While Not RS.EOF
Row = Row + 1
For Findex = 0 To RS.Fields.Count - 1
If Row >= Rows.Count - 50 Then
Exit For
End If
Data.Cells(Row + 1, Findex + 1) = RS.Fields(Findex).Value
Next Findex
RS.MoveNext
Loop
End If
Cells.EntireColumn.AutoFit
End Sub