{"id":3621,"date":"2013-07-15T10:21:07","date_gmt":"2013-07-15T02:21:07","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=3621"},"modified":"2018-06-24T18:27:11","modified_gmt":"2018-06-24T10:27:11","slug":"visual-basic-2012-lesson-31-working-with-databases-part-3","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-31-working-with-databases-part-3\/","title":{"rendered":"Visual Basic 2012 Lesson 31: Working with Databases Part 3"},"content":{"rendered":"<h4 style=\"text-align: center;\"><strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-30-working-with-databases-part-2\/\">[Lesson 30]<\/a>\u00a0<\/strong>&lt;&lt;\u00a0<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a><\/strong>&gt;&gt;<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/vb2010me\/\">[Next]<\/a><\/strong><\/h4>\n<p>In previous lessons, you have learned how to connect to a database as well as filling up the table with data in Visual Basic 2012, now you shall learn how to manipulate data in the database. Manipulating data means adding news records, editing records, deleting records, browsing records and more.<\/p>\n<h2><strong>31.1 Browsing Records\u00a0<\/strong><\/h2>\n<p>In the previous lesson, we have learned how to display the first record using the showRecords sub procedure. \u00a0In this lesson, we will create command buttons and write relevant codes to allow the user to browse the records forward and backward as well as fast forward to the last record and back to the first record.The first button we need to create is for the user to browse the first record. We can use button&#8217;s text <strong>&lt;&lt;\u00a0<\/strong>to indicate to the user that it is the button to move to the first record and button&#8217;s text &gt;&gt; to move to the last record. \u00a0Besides we can use button&#8217;s text &lt; \u00a0for moving to previous record and button&#8217;s text &gt; \u00a0for moving to next record.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>The code for moving to the first record is:<\/p>\n<pre style=\"font-size: 110%;\"><strong>MyRowPosition = 0<\/strong>\r\n<strong> Me.showRecords()<\/strong>\r\nThe code for moving to previous record is:\r\n<strong>If MyRowPosition &gt; 0 Then<\/strong>\r\n<strong> MyRowPosition = MyRowPosition - 1<\/strong>\r\n<strong> Me.showRecords()<\/strong>\r\n<strong> End If<\/strong>The code for moving to next record is:\r\n\r\n<strong>If MyRowPosition &lt; (MyDataTbl.Rows.Count - 1) Then<\/strong>\r\n<strong> MyRowPosition = MyRowPosition + 1<\/strong>\r\n<strong> Me.showRecords()<\/strong>\r\n<strong>End If<\/strong>\r\n<\/pre>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"1777484012\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>The code for moving to the last record is:<\/p>\n<pre style=\"font-size: 110%;\"><strong>If MyDataTbl.Rows.Count &gt; 0 Then<\/strong>\r\n<strong> MyRowPosition = MyDataTbl.Rows.Count - 1<\/strong>\r\n<strong> Me.showRecords()<\/strong>\r\n<strong> End If<\/strong>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>31.2 \u00a0Editing, Saving, Adding and Deleting Records<\/strong><\/h3>\n<p>You can edit any record by navigating to the record and change the data values. However, you need to save the data after editing them. You need to use the update method of the SqlDataAdapter to save the data. The code \u00a0is:<\/p>\n<pre style=\"font-size: 110%; width: 70%;\"><strong>If MyDataTbl.Rows.Count &lt;&gt; 0 Then<\/strong>\r\n<strong>MyDataTbl.Rows(MyRowPosition)(\"ContactName\") = txtName.Text<\/strong>\r\n<strong> MyDataTbl.Rows(MyRowPosition)(\"state\") = txtState.Text<\/strong>\r\n<strong> MyDatAdp.Update(MyDataTbl)<\/strong>\r\n<strong> End If<\/strong>\r\n<\/pre>\n<p>You can also add a new record or new row to the table using the following code:<\/p>\n<pre style=\"font-size: 110%; width: 70%;\"><strong>Dim MyNewRow As DataRow = MyDataTbl.NewRow()<\/strong>\r\n<strong> MyDataTbl.Rows.Add(MyNewRow)<\/strong>\r\n<strong> MyRowPosition = MyDataTbl.Rows.Count - 1<\/strong>\r\n<strong> Me.showRecords()<\/strong>\r\n<\/pre>\n<p>The code above will present a new record with blank fields for the user to enter the new data. After entering the data, he or she can then click the Save button to save the data.<\/p>\n<p>Lastly, the user might want to delete the data. The code to delete the data is:<\/p>\n<pre style=\"font-size: 110%; width: 70%;\"><strong>If MyDataTbl.Rows.Count &lt;&gt; 0 Then<\/strong>\r\n<strong> MyDataTbl.Rows(MyRowPosition).Delete()<\/strong>\r\n<strong> MyDatAdp.Update(MyDataTbl)<\/strong>\r\n<strong> MyRowPosition = 0<\/strong>\r\n<strong> Me.showRecords()<\/strong>\r\n<strong> End If<\/strong>\r\n<\/pre>\n<p>The Visual Basic 2012 database program interface is shown below:<\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-3610\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg\" alt=\"vb2012_fig30\" width=\"341\" height=\"305\" \/><\/a><\/p>\n<p>Finally, you have learned how to create a database application in Visual Basic 2012. Please try to create your very own database applications in Visual Basic 2012 from now on.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-format=\"autorelaxed\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"6598395509\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\">\u00a0<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-30-working-with-databases-part-2\/\">[Lesson 30]<\/a>\u00a0<\/strong>&lt;&lt;\u00a0<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-tutorial\/\">[CONTENTS]<\/a><\/strong>&gt;&gt;<strong><a href=\"http:\/\/www.vbtutor.net\/index.php\/vb2010me\/\">[Next]<\/a><\/strong><\/h4>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 30]\u00a0&lt;&lt;\u00a0[CONTENTS]&gt;&gt;[Next] In previous lessons, you have learned how to connect to a database as well as filling up the table with data in Visual Basic 2012, now you shall learn how to manipulate data in the database. Manipulating data means adding news records, editing records, deleting records, browsing records and more. 31.1 Browsing Records\u00a0 &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-31-working-with-databases-part-3\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2012 Lesson 31: Working with Databases Part 3<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-3621","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2012 Lesson 31: Working with Databases Part 3 - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This page explains how to create database applications in visual basic 2012\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2012 Lesson 31: Working with Databases Part 3 - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This page explains how to create database applications in visual basic 2012\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-24T10:27:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-31-working-with-databases-part-3\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html\",\"name\":\"Visual Basic 2012 Lesson 31: Working with Databases Part 3 - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg\",\"datePublished\":\"2013-07-15T02:21:07+00:00\",\"dateModified\":\"2018-06-24T10:27:11+00:00\",\"description\":\"This page explains how to create database applications in visual basic 2012\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg\",\"width\":341,\"height\":305},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2012 Lesson 31: Working with Databases Part 3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2012 Lesson 31: Working with Databases Part 3 - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This page explains how to create database applications in visual basic 2012","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2012 Lesson 31: Working with Databases Part 3 - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This page explains how to create database applications in visual basic 2012","og_url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-24T10:27:11+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2012-lesson-31-working-with-databases-part-3\/","url":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html","name":"Visual Basic 2012 Lesson 31: Working with Databases Part 3 - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg","datePublished":"2013-07-15T02:21:07+00:00","dateModified":"2018-06-24T10:27:11+00:00","description":"This page explains how to create database applications in visual basic 2012","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2013\/07\/vb2012_fig30.jpg","width":341,"height":305},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2012\/vb2012_lesson31.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2012 Lesson 31: Working with Databases Part 3"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/3621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=3621"}],"version-history":[{"count":25,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/3621\/revisions"}],"predecessor-version":[{"id":13123,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/3621\/revisions\/13123"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=3621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=3621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=3621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}