ADO Recordset Not Staying Open in VBA - “Operation is not allowed when the object is closed”

后端 未结 3 884
我在风中等你
我在风中等你 2021-01-21 07:41

I don\'t really understand what has happened here. I\'m using Excel VBA to connect to a SQL Server Express database and return an ADO Recordset. I had it working initially, but

3条回答
  •  执念已碎
    2021-01-21 08:25

    This is a bit long for a comment so I'll put it here as a possible answer. Please try:

    Dim rs As ADODB.Recordset
    Set rs = New ADODB.Recordset
    With rs
        Set .ActiveConnection = conn
        .LockType = adLockOptimistic
        .CursorLocation = adUseServer
        .CursorType = adOpenForwardOnly
        .Open "SET NOCOUNT ON"
    End With
    rs.Open Cmd, , , , adCmdStoredProc
    
    MsgBox ("Success! " & rs.RecordCount & " Records Returned!")
    

提交回复
热议问题