Thread: Excel ADO Crash
View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
TK TK is offline
external usenet poster
 
Posts: 177
Default Excel ADO Crash

Hi George:

Sorry Im not much help here but I tested your code by connecting to
Access "as you can see by the revised code" I could not fail the code
retrieving a rs into excel. That should narrow the issue to your select
statement or login.
One thing in your procedu If you use the New (keyword) you do not
then need to use the Set (keyword ) because the variable has been initialize.
I think the old line is still to Dim then Set, of course it made no difference
in testing your procedure.

'op test 11/18/04

Private Sub CommandButton16_Click()

' Sub ImportDB()
' Imports data from Access database through ADO

'Dim cnt As New ADODB.Connection
Dim cnt As ADODB.Connection

'Dim rst As New ADODB.Recordset
Dim rst As ADODB.Recordset

Dim strDB As String
Dim fldCount As Integer
Dim iCol As Integer

' Set the string to the name of the database
strDB = ("C:\Program Files\Microsoft Visual Studio\VB98\NWind.mdb")
'''"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 * from products", cnt '//' "SELECT QTR, SUM(PREMIUM) From
[George] GROUP BY QTR", cnt
'Rs.Open strSql, cnn, adOpenStatic, adLockBatchOptimistic

' 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(1, 1).CopyFromRecordset rst

' Close ADO objects
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing

End Sub

Good Luck
TK