{"id":12096,"date":"2011-11-25T14:53:00","date_gmt":"2011-11-25T14:53:00","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=812"},"modified":"2022-08-06T12:11:55","modified_gmt":"2022-08-06T12:11:55","slug":"sql-connection-string","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2011\/11\/sql-connection-string\/","title":{"rendered":"SQL Connection using Excel Macros"},"content":{"rendered":"
Dear Friends,<\/p>\n
Using Excel Macros (VBA) you can connect to Databases like SQL, Oracle or Access DB. In this Article, I am going to teach you, how you can do a connection with a SQL Server. 1. Go to VB Editor Screen (Alt+F11)<\/span> SQL Connection String – Using SQL Server Authentication<\/a> <\/a><\/p>\n <\/a><\/p>\n Dear Friends, Using Excel Macros (VBA) you can connect to Databases like SQL, Oracle or Access DB. In this Article, I am going to teach you, how you can do a connection with a SQL Server. SQL databases has mainly two type of authentication : 1. Windows Authentication 2. SQL Server authentication. Here in this […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[5203,1246,1675],"tags":[],"class_list":["post-12096","post","type-post","status-publish","format-standard","hentry","category-database","category-macro","category-excel-macro-for-beginners"],"yoast_head":"\n
\nSQL databases has mainly two type of authentication :
\n1.<\/strong> Windows Authentication <\/em>
\n2.<\/strong> SQL Server authentication.<\/em>
\nHere in this article, I will share VBA code for both type of connection – Windows authentication and SQL Server Authentication.
\nBefore you start running the below code, make sure that reference for ADODB Connection is added in your Excel Workbook.
\nIf you do not know how to add references to your excel workbook, refer the below steps:<\/p>\nHow to add ADODB Connection references in Excel<\/h2>\n
\n 2. Tools –> References…<\/strong><\/span><\/span>
\n 3. From the List of Available References Select “Microsoft ActiveX Data Objects 2.0 Library<\/span>” <\/strong> You can select 2.0 Version or any other higher version of this reference.<\/span>
\n 4. Click OK<\/span><\/p>\n<\/div>\n
\nSQL Connection String – Using Windows Authentication<\/a><\/p>\nUsing SQL Server Authentication: <\/h2>\n
\r\n
\r\n\r\n Sub SQL_Connection() \r\n \r\n Dim con As ADODB.Connection \r\n Dim rs As ADODB.Recordset \r\n Dim query As String \r\n Set con = New ADODB.Connection \r\n Set rs = New ADODB.Recordset \r\n \r\n '---- Replace below highlighted names with the corresponding values \r\n \r\n strCon = \"Provider=SQLOLEDB; \" & _\r\n \"Data Source=\"your ServerName\"; \" & _\r\n \"Initial Catalog=database name;\" & _\r\n \"User ID=uid; Password=pwd; Trusted_Connection=yes\" \r\n \r\n '--- Open the above connection string. \r\n \r\n con.Open (strCon) \r\n \r\n '--- Now connection is open and you can use queries to execute them.\r\n '--- It will be open till you close the connection \r\n \r\n End Sub \r\n<\/code>\r\n<\/code><\/pre>\n
Using Windows Authentication: <\/h2>\n
\r\n
\r\n\r\n Sub SQL_Connection() \r\n \r\n Dim con As ADODB.Connection \r\n Dim rs As ADODB.Recordset \r\n Dim query As String \r\n Set con = New ADODB.Connection \r\n Set rs = New ADODB.Recordset \r\n \r\n '---- Replace below highlighted names with the corresponding values \r\n \r\n strCon = \"Provider=SQLOLEDB; \" & _\r\n \"Data Source=\"your ServerName\"; \" & _\r\n \"Initial Catalog=database name;\" & _\r\n \"Integrated Security=SSPI\" \r\n \r\n '--- Open the above connection string. \r\n \r\n con.Open (strCon) \r\n \r\n '--- Now connection is open and you can use queries to execute them.\r\n '--- It will be open till you close the connection \r\n \r\n End Sub \r\n<\/code>\r\n<\/code><\/pre>\n
Read this Also:<\/h2>\n
\n