title: SQL Server 2008 data to Excel with VB6
body: <p>I have written this code for extract button. It works perfectly. What I'd like to do is have the Excel app open and show the data in the sheet. For now after every extract I have to go to the <code>c:</code> drive in order to see the spreadsheet. Also, is there a way to autofit the columns, change header color. Thanks!</p>
  
  <pre><code>Dim FileNum As Integer
  Dim strSQL As String
  Dim rs As ADODB.Recordset
  Dim strFileName As String
  
  FileNum = FreeFile
  strFileName = "C:\Test\TESTFILE.CSV"
  
  Open strFileName For Output As #FileNum
  Write #FileNum, "ID", "FirstName", "LastName", "DOB", "SSN", "SEX"
  
  Set rs = New ADODB.Recordset
  
  strSQL = "Select ID, FirstName, LastName, DOB, SSN, Sex from         dbo.tblTest_Clients Where ID = " &amp; g_lngSelectedID
  
  rs.Open strSQL, g_cnDatabase
  rs.MoveFirst
  
  Do While Not rs.EOF
  Write #FileNum, rs("ID") &amp; vbTab; rs("FirstName") &amp; vbTab; rs("LastName") &amp;    vbTab; rs("DOB") &amp; vbTab; rs("SSN") &amp; vbTab; rs("Sex")
  rs.MoveNext
  Loop
  
  rs.Close
  Close #FileNum
  </code></pre>