How to acces ADO disconnected recordset object from another workbook

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
Is it possible to have a recordset object created in one workbook but access this recordset object from another workbook's vba code? I was thinking maybe getobject could be used to capture it from another workbook but I cannot find any resources on this.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Yes, it's possible.

Put this code to a module of the source workbook WbSource.xlsb
Rich (BB code):
' Code in the source workbook "WbSource.xlsb"
Option Explicit

Function DisconnectedRs() As Object
  
  Set DisconnectedRs = CreateObject("ADODB.Recordset")
  
  With DisconnectedRs
    
    ' Add some fields
    .Fields.Append "ID", 3
    .Fields.Append "Value", 129, 20
    
    'Open it up
    .Open , , 3, 3
    
    'Add new record
    .AddNew
    'Put values
    .Fields(0).Value = 1
    .Fields(1).Value = "Description1"
    
    'Add new record
    .AddNew
    'Put values
    .Fields(0).Value = 2
    .Fields(1).Value = "Description2"

    'Pass it for the fast updating (without using .Update for each record)
    .MoveFirst
    
  End With

End Function

The code below goes to a module of the destination workbook:
Rich (BB code):
' Code in the destination workbook "WbDest.xlsb"
Option Explicit

Dim Rs As Object 'Recordset

Sub GetExternalRs()
  Set Rs = Application.Run("'WbSource.xlsb'!DisconnectedRs")
  ThisWorkbook.Sheets(1).Range("A2").CopyFromRecordset Rs
End Sub

Then run GetExternalRs() to see it is working.

Regards
 
Last edited:
Upvote 0
This is great if you want to create the disconnected recordset from another workbook and utilize it in either workbook which is really cool too. However, Is there a way to locate the object in memory if it was already created in the other workbook and use it in the other which did not create it?
 
Last edited:
Upvote 0
Looking at your example. I thought this would be the solution but I hit a roadblock because how would the user find the recordset to set it in the function.

Code:
' Code in the source workbook "WbSource.xlsb"
Option Explicit
'ado recordset created somewhere in this module

Function DisconnectedRs() As ADODB.Recordset
   'If we could find all the disconnected recordset or loop through them somehow? IDK how to accomplish this.
  Set DisconnectedRs = DisconnectedRecordset 

End function

The code below goes to a module of the destination workbook:
Code:
Code:
' Code in the destination workbook "WbDest.xlsb"
Option Explicit


Dim Rs As Object 'Recordset


Sub GetExternalRs()
  Set Rs = Application.Run("'WbSource.xlsb'!DisconnectedRs")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,047
Members
448,940
Latest member
mdusw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top