|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, June 22, 2011 8:36 AM
Points: 28,
Visits: 6
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, June 22, 2011 8:36 AM
Points: 28,
Visits: 6
|
|
Regarding the problem with outputing to Word. The DBName has nulls. We used an If..Then to detect the Null, and output "no db" else the name of the database. If anyone is interested I can e-mail the code.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, May 21, 2012 2:42 AM
Points: 127,
Visits: 39
|
|
And adding a short inputbox for the server is cool if your centrally managing multiple servers. [...] Dim i Dim srvname srvname = InputBox ( "Enter the Server Name", "Server", "xxxxxx") ' making the connection to your sql server ' change yourservername to match your server Set objConnection = CreateObject("ADODB.Connection") Set objRecordSet = CreateObject("ADODB.Recordset") ' this is using the trusted connection if you use sql logins ' add username and password, but I would then encrypt this ' using Windows Script Encoder objConnection.Open _ "Provider = SQLOLEDB;Data Source=" & srvname & " ;" & _ "Trusted_Connection=Yes;Initial Catalog=Master;" [...] And this little addition formats the CPUTime column so that you can sort the column afterwards: [...] objExcel.Cells(i, 7).Value = FormatNumber(objRecordset.Fields.Item("CPUTime"), 0, -1, 0, 0) [...] Otherwise a cool piece of code. I don't have the time to get into coding much these days, but that might change. Thanks hot2use
__________________________________ Searching the KB articles is like picking your nose. You never know what you'll find.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Yesterday @ 7:07 AM
Points: 9,
Visits: 471
|
|
I called objRecordSet rs and objWorksheet ws, but it seems like you could just do something like this
For CurCol = 0 To rs.Fields.Count - 1: ws.Cells(1, CurCol + 1).Value = rs.Fields(CurCol).Name: Next ws.Range("A2").CopyFromRecordset rs
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, August 18, 2005 4:21 PM
Points: 75,
Visits: 1
|
|
| Why do you have the banner (row 1) cells defined in the loop?
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Yesterday @ 7:07 AM
Points: 9,
Visits: 471
|
|
Just as the column names, for use with pivot tables. I use this code to automate reporting to excel. A few lines of code hoses the data to excel & refreshes all the pivot tables & graphs in the workbook...
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, June 22, 2011 8:36 AM
Points: 28,
Visits: 6
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Wednesday, June 22, 2011 8:36 AM
Points: 28,
Visits: 6
|
|
You can remove the titles from the loop.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, April 19, 2011 1:27 AM
Points: 116,
Visits: 59
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Yesterday @ 7:07 AM
Points: 9,
Visits: 471
|
|
you can replace the entire second loop with 1 command:
objWorksheet.Range("A2").CopyFromRecordset objRecordSet
|
|
|
|