{"id":158,"date":"2014-02-21T17:34:34","date_gmt":"2014-02-22T00:34:34","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=158"},"modified":"2014-12-09T13:20:04","modified_gmt":"2014-12-09T20:20:04","slug":"db-interface","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/vba\/appdesign\/db-interface\/","title":{"rendered":"Connecting to MS Access"},"content":{"rendered":"<p>Here we are going to see how to connect to a database in Access using Excel VBA. Separating data and interface is a common practice in application development as it has the following benefits:<\/p>\n<ul>\n<li><em>Data Sharing<\/em>: multiple users can view and handle the same information. This is called concurrency a main property of databases!<\/li>\n<li><em>Improved performance<\/em>: users store only the required data on their pc, therefore connection is faster and data availability increases.<\/li>\n<li><em>Data security<\/em>: having data stored in centralized computer allows more control. Also, if there is any problem in the interface there is less probability of corrupting the database.<\/li>\n<li><em>Development Flexibility<\/em>: new interface versions can be developed without disrupting access to data.<\/li>\n<\/ul>\n<h2>Connecting to an Access DB \u000bUsing Excel VBA<\/h2>\n<p><strong>1. Setting connection<\/strong><\/p>\n<p>a. Set path name<\/p>\n<p style=\"padding-left: 30px;\">Dim PthName As String<br \/>\nPthName = ActiveWorkbook.Path &#8216;if DB and workbook are in same folder<\/p>\n<p>b. Set database name<\/p>\n<p style=\"padding-left: 30px;\">Dim DbName As String<br \/>\nDBName = \u201cDatabaseFile.mdb\u201d<\/p>\n<p>c. Set connection to DB<\/p>\n<p style=\"padding-left: 30px;\">MS Office \u201897-03<br \/>\nDim Conn As New ADODB.Connection<br \/>\nConn.Open &#8220;Provider=Microsoft.Jet.OLEDB.4.0; Data Source=&#8221; &amp; PthName &amp; DbName &amp; &#8220;;\u201c<\/p>\n<p style=\"padding-left: 30px;\">MS Office 2007 \/2010<br \/>\nDim Conn As New ADODB.Connection<br \/>\nConn.Open &#8220;Provider=Microsoft.ACE.OLEDB.12.0; Data Source=&#8221; &amp; PthName &amp; DbName &amp; &#8220;;&#8221;<br \/>\nFirst you have to add ActiveX data objects 2.8 in your project references (Tools menu -&gt; references)<\/p>\n<p><strong>2. Execute SQL Command<\/strong><\/p>\n<p>a. Set string for SQL command<\/p>\n<p style=\"padding-left: 30px;\">Dim strSQL as String<br \/>\nstrSQL = \u201cSELECT * FROM Students\u201d<\/p>\n<p>b.i. Execute create, update and delete commands<\/p>\n<p style=\"padding-left: 30px;\">Conn.Execute strSQL<\/p>\n<p>\u00a0b.ii. Execute query command<\/p>\n<p style=\"padding-left: 30px;\">Dim RecSet As New ADODB.Recordset<br \/>\nRecSet.Open strSQL, Conn<\/p>\n<p>\u00a0Example: insert new student record into Students table<\/p>\n<p>Dim strSQL\u000bstrSQL = &#8220;INSERT INTO Students (StudentName, Program, AdmYear) &#8221; &amp; _\u000b &#8220;VALUES (&#8220;&#8221;&#8221; &amp; stgStrNa &amp; &#8220;&#8221;&#8221;,&#8221;&#8221;&#8221; &amp; StrPr &amp; &#8220;&#8221;&#8221;,&#8221;&#8221;&#8221; &amp; StrAY &amp; &#8220;&#8221;&#8221;); &#8220;\u000bConn.Execute strSQL<\/p>\n<p><strong>3. Return Data (Queries)<\/strong><\/p>\n<p><strong><\/strong>Putting result set into an Array makes data processing easier<\/p>\n<p style=\"padding-left: 30px;\">Dim ResultArray as Variant<br \/>\nResultArray = RecSet.GetRows<br \/>\nTotalRows = Ubound(ResultArray, 2)<\/p>\n<p><strong>4. Close Connection<\/strong><\/p>\n<p>a. Close the recordset<\/p>\n<p style=\"padding-left: 30px;\">RecSet.Close<br \/>\nRecSet = Nothing<\/p>\n<p>b. Close the connection<\/p>\n<p style=\"padding-left: 30px;\">Conn.Close<br \/>\nConn = Nothing<\/p>\n<p><strong>Handling Connection Errors<\/strong><\/p>\n<p>If an SQL command is incorrect or connection can\u2019t be established VBA will generate an error. If this happens we need to close connection and recordset.<\/p>\n<p>On Error GoTo ErrorCloseConnection<br \/>\n\u2018<br \/>\n\u2018Connection commands here<br \/>\n\u2018<\/p>\n<p>ErrorCloseConnection:<\/p>\n<p>RecSet.Close<br \/>\nSet RecSet = Nothing<\/p>\n<p>Conn.Close<br \/>\nSet Conn = Nothing<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we are going to see how to connect to a database in Access using Excel VBA. Separating data and interface is a common practice in application development as it has the following benefits: Data Sharing: multiple users can view and handle the same information. This is called concurrency a main property of databases! Improved [&hellip;]<\/p>\n","protected":false},"author":22982,"featured_media":0,"parent":78,"menu_order":3,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"footnotes":""},"class_list":["post-158","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/22982"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":8,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/158\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/158\/revisions\/183"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/78"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}