VBA - Create Data Connection when CommandText is Too Long

jintingljw

New Member
Joined
Apr 12, 2016
Messages
2
Hi,

Usually, I use below VBA to create Data Connection with SQL database to extract data through MS Query. And it works perfectly fine.

As defined, SQL_CommandText is a string where you can set your SQL.

However, I get a huge SQL that is around 16,000 rows (40,000 characters). This VBA code doesn't works.

Assuming I don't want to modify the SQL, is there any way I can Create Data Connection when CommandText is Too Long?

Thank you for your help.

Jason



Sub ExtractData()


Dim SQL_NAME as String
Dim SQL_CONNECTION as String
Dim SQL_CommandText as String


'Refresh Connection
ConnectionName = SQL_NAME


ActiveWorkbook.Connections(ConnectionName).ODBCConnection.Connection = SQL_CONNECTION
ActiveWorkbook.Connections(ConnectionName).ODBCConnection.CommandText = SQL_CommandText
ActiveWorkbook.Connections(ConnectionName).REFRESH

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,223,053
Messages
6,169,831
Members
452,284
Latest member
TKM623

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