Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
hi
i wrote a macro to connect to a database using ADO to retrieve some records Private Sub Display() Dim sql As String Dim con As ADODB.Connection Dim rs1 As ADODB.recordset sql = "select * from table" MsgBox sql Set con = New ADODB.Connection Set rs1 = New ADODB.recordset con.Open "DSN=" & DSN & ";Database=" & DB & ";UID=" _ & User & ";PWD=" & Password rs1.Open sql, con If Not rs1.EOF Then <<---debugger points here.. MsgBox "Yes" Else MsgBox "No" End If rs1.Close con.Close End Sub Whenever i run this, i would get error "Operation is not allowed when the object is closed." and the debugger will point to the statement "If Not rs1.EOF Then...". I tried using "Do Until rs1.EOF" but the error is the same.. Can anyone advise me on what is wrong..thanks very much |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Mike
It looks pretty good from here, givetn that User, Database and Password are declared and correct. I can't test it without the real things present. My first guess would be that it doesn't open for some reason but fails to report the error. Doublecheck your connection string at Carl Prothman's site: http://www.carlprothman.net/Default.aspx?tabid=81 Also, you haven't provided cursor and lock. That is always a good thing to do: rs1.Open sql, con, adOpenForwardOnly, adLockReadOnly HTH. Best wishes Harald "mike" skrev i melding oups.com... hi i wrote a macro to connect to a database using ADO to retrieve some records Private Sub Display() Dim sql As String Dim con As ADODB.Connection Dim rs1 As ADODB.recordset sql = "select * from table" MsgBox sql Set con = New ADODB.Connection Set rs1 = New ADODB.recordset con.Open "DSN=" & DSN & ";Database=" & DB & ";UID=" _ & User & ";PWD=" & Password rs1.Open sql, con If Not rs1.EOF Then <<---debugger points here.. MsgBox "Yes" Else MsgBox "No" End If rs1.Close con.Close End Sub Whenever i run this, i would get error "Operation is not allowed when the object is closed." and the debugger will point to the statement "If Not rs1.EOF Then...". I tried using "Do Until rs1.EOF" but the error is the same.. Can anyone advise me on what is wrong..thanks very much |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Confusion about how the Window object fits into the Excel object model | Excel Programming | |||
"operation is not allowed when the object is closed" error when us | Excel Programming | |||
Recordset or Object is closed HELP PLEASE! | Excel Programming | |||
Recordset Problem - object is closed | Excel Programming | |||
Operation Is Not Allowed When The Object Is Closed | Excel Programming |