Connecting Tech Pros Worldwide Forums | Help | Site Map

VB6 and Excel

Member
 
Join Date: Aug 2006
Posts: 63
#1: Dec 12 '06
I have the following code to populate an excel spreadsheet from vb6 and access database. It works great except that when it reads from the recordset and populates the spreadsheet, I would like it to populate every other line in the spreadsheet. I can't seem to see anywhere in the code where that might be handled. Any help would be greatly appreciated.



Expand|Select|Wrap|Line Numbers
  1. Private Sub lblGroundScheduleExcel_Click()
  2.     Dim cnt As New ADODB.Connection
  3.     Dim rst As New ADODB.Recordset
  4.  
  5.     Dim xlApp As Object
  6.     Dim xlWb As Object
  7.     Dim xlWs As Object
  8.  
  9.  
  10.     Dim recArray As Variant
  11.  
  12.     Dim strDB As String
  13.     Dim fldCount As Integer
  14.     Dim recCount As Long
  15.     Dim iCol As Integer
  16.     Dim iRow As Integer
  17.  
  18.     ' Set the string to the path of your Northwind database
  19.     strDB = "c:\scheduling\AAGTC_Scheduling.mdb"
  20.  
  21.     ' Open connection to the database
  22.     cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  23.         "Data Source=" & strDB & ";"
  24.  
  25.     ' Open recordset based on Orders table
  26.     rst.Open "Select Date, Unit, P_O_C, Mission, Vehicle_Qty, Personnel_Daily_Report, [Range Areas], Facility_Useage_For_Daily_Report, Daily_Ordinance, Comments From qryDailyGroundScheduleReport", cnt
  27.  
  28.     ' Create an instance of Excel and add a workbook
  29.     Set xlApp = CreateObject("Excel.Application")
  30.     Set xlWb = xlApp.Workbooks.Add("C:\Scheduling\Excel\Deployment_Activity1.xls")
  31.     Set xlWs = xlWb.Worksheets("2007")
  32.  
  33.     ' Display Excel and give user control of Excel's lifetime
  34.     xlApp.Visible = True
  35.     xlApp.UserControl = True
  36.  
  37.     ' Copy field names to the first row of the worksheet
  38.    ' fldCount = rst.Fields.Count
  39.    ' For iCol = 1 To fldCount
  40.      '   xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
  41.     'Next
  42.  
  43.     ' Check version of Excel
  44.     If Val(Mid(xlApp.Version, 1, InStr(1, xlApp.Version, ".") - 1)) > 8 Then
  45.         'EXCEL 2000 or 2002: Use CopyFromRecordset
  46.  
  47.         ' Copy the recordset to the worksheet, starting in cell A2
  48.         xlWs.Cells(3, 1).CopyFromRecordset rst
  49.         'Note: CopyFromRecordset will fail if the recordset
  50.         'contains an OLE object field or array data such
  51.         'as hierarchical recordsets
  52.  
  53.     Else
  54.         'EXCEL 97 or earlier: Use GetRows then copy array to Excel
  55.  
  56.         ' Copy recordset to an array
  57.         recArray = rst.GetRows
  58.         'Note: GetRows returns a 0-based array where the first
  59.         'dimension contains fields and the second dimension
  60.         'contains records. We will transpose this array so that
  61.         'the first dimension contains records, allowing the
  62.         'data to appears properly when copied to Excel
  63.  
  64.         ' Determine number of records
  65.  
  66.         recCount = UBound(recArray, 2) + 1 '+ 1 since 0-based array
  67.  
  68.  
  69.         ' Check the array for contents that are not valid when
  70.         ' copying the array to an Excel worksheet
  71.         For iCol = 0 To fldCount - 1
  72.             For iRow = 0 To recCount - 1
  73.                 ' Take care of Date fields
  74.                 If IsDate(recArray(iCol, iRow)) Then
  75.                     recArray(iCol, iRow) = Format(recArray(iCol, iRow))
  76.                 ' Take care of OLE object fields or array fields
  77.                 ElseIf IsArray(recArray(iCol, iRow)) Then
  78.                     recArray(iCol, iRow) = "Array Field"
  79.                 End If
  80.             Next iRow 'next record
  81.         Next iCol 'next field
  82.  
  83.         ' Transpose and Copy the array to the worksheet,
  84.         ' starting in cell A2
  85.         xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = _
  86.             TransposeDim(recArray)
  87.     End If
  88.  
  89.     ' Auto-fit the column widths and row heights
  90.     xlApp.Selection.CurrentRegion.Columns.AutoFit
  91.     xlApp.Selection.CurrentRegion.Rows.AutoFit
  92.  
  93.     ' Close ADO objects
  94.     rst.Close
  95.     cnt.Close
  96.     Set rst = Nothing
  97.     Set cnt = Nothing
  98.  
  99.     ' Release Excel references
  100.     Set xlWs = Nothing
  101.     Set xlWb = Nothing
  102.  
  103.     Set xlApp = Nothing
  104.  
  105. End Sub
  106.  
Reply