Excel VBA Joining Tables on Different Servers

The_Kurgan

Active Member
Joined
Jan 10, 2006
Messages
270
I realize this is probably a long shot, but would anyone have sample VBA SQL code where you're joining tables from different servers? I've done a lot with pulling data from tables on one server, but not more.

TIA


For some frame of reference only, this is some sample code where I'm adding column names from a chosen table into a listbox (no joining... just a straight query).
Code:
Set objMyConn = New ADODB.Connection
Set objMyRecordset = New ADODB.Recordset
Dim strSQL As String
'Open Connection'
objMyConn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=" & MyServer & ";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=SECONDARYTS;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=" & MyDB
objMyConn.Open
'Set and Excecute SQL Command'
strSQL = "SELECT column_name FROM information_schema.columns WHERE table_name = '" & DB_Data.ListBox1.Text & " ';"
'Open Recordset'
Set objMyRecordset.ActiveConnection = objMyConn
objMyRecordset.Open strSQL
'Copy Data to Excel'
objMyRecordset.MoveFirst
iter = 0
Do Until objMyRecordset.EOF = True
    DB_Data.ListBox2.AddItem objMyRecordset(0)
    objMyRecordset.MoveNext
    iter = iter + 1
Loop
objMyConn.Close
Set objMyRecordset = Nothing
Set objMyConn = Nothing
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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