vba - sql string

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
879
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am using below code for extracting SQL Data into into excel and it worked.


Below is connection string I created using recording(extract data from external source)
Any alternate way and where we can shorten the connection string.


Const conSTRSQL As String = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=DESKTOP-VJ0FV5Q\SQLEXPRESS;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=DESKTOP-VJ0FV5Q;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=master"



SQL:
Option Explicit

Const conSTRSQL As String = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=DESKTOP-VJ0FV5Q\SQLEXPRESS;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=DESKTOP-VJ0FV5Q;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=master"

Sub Extract_SQL_Data()

Dim cn As ADODB.Connection
Dim rst As ADODB.Recordset

Set cn = New ADODB.Connection
Set rst = New ADODB.Recordset


cn.ConnectionString = conSTRSQL

cn.Open



With rst
    .ActiveConnection = cn
    .Source = "Persons"
    .LockType = adLockReadOnly
    .CursorType = adOpenForwardOnly
    .Open
End With


Worksheets.Add
Range("A2").CopyFromRecordset rst


cn.Close

1614453379045.png


End Sub
 

Attachments

  • 1614453366693.png
    1614453366693.png
    9.1 KB · Views: 2

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Watch MrExcel Video

Forum statistics

Threads
1,133,244
Messages
5,657,582
Members
418,401
Latest member
B_A_M155

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
Top