vba - sql string

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
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: 12

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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