473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

VB6 and Excel

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.  
Dec 12 '06 #1
0 3601

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
9
by: Anthony | last post by:
To me, creating Excel 2003 spreadsheets programmatically via VB.NET hasn't really changed since the days of VB6. That is, I'd do something similar to this Code: Dim ExcelApp As...
7
by: Alain \Mbuna\ | last post by:
Hi everybody. In my program I have some data that is calculated after some input from the user. I have written some code that opens an Excel workbook, with 5 worksheets and the calculated data...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.