Re: Excel ADO Crash
From: George (George_at_discussions.microsoft.com)
Date: 11/18/04
- Next message: Ron de Bruin: "Re: Printing date range in excel2000"
- Previous message: Geoff C: "RE: Change control methods"
- In reply to: Jamie Collins: "Re: Excel ADO Crash"
- Next in thread: TK: "Re: Excel ADO Crash"
- Reply: TK: "Re: Excel ADO Crash"
- Reply: Jamie Collins: "Re: Excel ADO Crash"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 18 Nov 2004 05:56:01 -0800
Below is the VBA code I am using. One other important point: the recordset
[George] is actually a pass-through query that retrieves data from a backend
DB2 database. The first time, it works like a charm; it prompts me for a
userid and password, and retrieves the data into Excel. The second time,
Excel dies.
Sub ImportDB()
' Imports data from Access database through ADO
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strDB As String
Dim fldCount As Integer
Dim iCol As Integer
' Set the string to the name of the database
strDB = "M:\George.mdb"
' Open connection to the database
Set cnt = New ADODB.Connection
cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDB & ";"
' Open recordset based on Orders table
Set rst = New ADODB.Recordset
rst.Open "SELECT QTR, SUM(PREMIUM) From [George] GROUP BY QTR", cnt
' Copy field names to the first row of the worksheet
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
ActiveSheet.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
Next
' Copy the recordset to the worksheet, starting in cell A2
ActiveSheet.Cells(2, 1).CopyFromRecordset rst
' Close ADO objects
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
End Sub
- Next message: Ron de Bruin: "Re: Printing date range in excel2000"
- Previous message: Geoff C: "RE: Change control methods"
- In reply to: Jamie Collins: "Re: Excel ADO Crash"
- Next in thread: TK: "Re: Excel ADO Crash"
- Reply: TK: "Re: Excel ADO Crash"
- Reply: Jamie Collins: "Re: Excel ADO Crash"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|