silentbuddha
Board Regular
- Joined
- Mar 1, 2008
- Messages
- 112
Hi,
I am having difficulties in getting the column headers from a MS SQL Server 2005 database table
Here is the code I have so far :
Private Sub getRetentionDetails(date1 As String, date2 As String)
'This was set up using Microsoft ActiveX Data Components version 2.8
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim strSQLHeaders As String
Dim wb As Workbook
Dim ws As Worksheet
Dim rangeStart As Range
Dim iCol As Integer
Const CONN_STRING As String = "Provider=SQLNCLI;Server=sql.XYZreporting;Database=Business_Analysis;Trusted_Connection=yes;HDR=yes"";"
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("RETENTION_DETAILS")
With ws
Set rangeStart = .Range("A2")
End With
strSQL = "select [Week],[Requests],[SRN],[%SWA],[Pending%],[Cost/Save with Agreement],[Cost/Save W-O Agreement],[Cost/Save Total],[Saves],[Deacts],[%Mig],[Mig],[Saves Net],[Renewals],[Renewals 2Y%],[Renewals 3Y%],[Pendings],[SRV]" & _
" from dbo.Consolidated_KPI_RetentionDetails " & _
" where calendar_date between " & "'" & date1 & "'" & " and " & "'" & date2 & "'" & _
" group by [Week],[Requests],[SRN],[%SWA],[Pending%],[Cost/Save with Agreement],[Cost/Save W-O Agreement],[Cost/Save Total],[Saves],[Deacts],[%Mig],[Mig],[Saves Net],[Renewals],[Renewals 2Y%],[Renewals 3Y%],[Pendings],[SRV]" & _
" order by [week]"
strSQLHeaders = "select top 1 * from dbo.Consolidated_KPI_RetentionDetails"
'part 1 : get the headers and place them into worksheet RETENTION_DETAILS
Set conn = New ADODB.Connection
With conn
.CursorLocation = adUseClient
.Open CONN_STRING
.CommandTimeout = 0
Set rs = .Execute(strSQLHeaders)
End With
For iCol = 1 To rs.Fields.Count
Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
Next
'cleanup and close connection
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
'***************** end of part 1 *****************'
Thanks in advance !
I am having difficulties in getting the column headers from a MS SQL Server 2005 database table
Here is the code I have so far :
Private Sub getRetentionDetails(date1 As String, date2 As String)
'This was set up using Microsoft ActiveX Data Components version 2.8
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL As String
Dim strSQLHeaders As String
Dim wb As Workbook
Dim ws As Worksheet
Dim rangeStart As Range
Dim iCol As Integer
Const CONN_STRING As String = "Provider=SQLNCLI;Server=sql.XYZreporting;Database=Business_Analysis;Trusted_Connection=yes;HDR=yes"";"
Set wb = ActiveWorkbook
Set ws = wb.Worksheets("RETENTION_DETAILS")
With ws
Set rangeStart = .Range("A2")
End With
strSQL = "select [Week],[Requests],[SRN],[%SWA],[Pending%],[Cost/Save with Agreement],[Cost/Save W-O Agreement],[Cost/Save Total],[Saves],[Deacts],[%Mig],[Mig],[Saves Net],[Renewals],[Renewals 2Y%],[Renewals 3Y%],[Pendings],[SRV]" & _
" from dbo.Consolidated_KPI_RetentionDetails " & _
" where calendar_date between " & "'" & date1 & "'" & " and " & "'" & date2 & "'" & _
" group by [Week],[Requests],[SRN],[%SWA],[Pending%],[Cost/Save with Agreement],[Cost/Save W-O Agreement],[Cost/Save Total],[Saves],[Deacts],[%Mig],[Mig],[Saves Net],[Renewals],[Renewals 2Y%],[Renewals 3Y%],[Pendings],[SRV]" & _
" order by [week]"
strSQLHeaders = "select top 1 * from dbo.Consolidated_KPI_RetentionDetails"
'part 1 : get the headers and place them into worksheet RETENTION_DETAILS
Set conn = New ADODB.Connection
With conn
.CursorLocation = adUseClient
.Open CONN_STRING
.CommandTimeout = 0
Set rs = .Execute(strSQLHeaders)
End With
For iCol = 1 To rs.Fields.Count
Cells(1, iCol).Value = rs.Fields(iCol - 1).Name
Next
'cleanup and close connection
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
'***************** end of part 1 *****************'
Thanks in advance !