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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,214,822
Messages
6,121,770
Members
449,049
Latest member
greyangel23

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