Learn to create database applications using Visual Basic Data Control with practical examples in both VB6 and VB.NET
Visual Basic provides powerful tools for creating database applications. Using the Data Control component, you can connect to databases, navigate records, and perform CRUD (Create, Read, Update, Delete) operations with minimal code.
A simple database navigation interface with bound controls and navigation buttons.
The VB6 Data Control handles the database connection and record navigation. You can use its methods to programmatically navigate records:
Private Sub cmdFirst_Click() ' Move to the first record dtaBooks.Recordset.MoveFirst End Sub Private Sub cmdNext_Click() ' Move to the next record dtaBooks.Recordset.MoveNext ' Check if we've reached the end If dtaBooks.Recordset.EOF Then dtaBooks.Recordset.MoveLast MsgBox "End of records!", vbInformation End If End Sub Private Sub cmdPrevious_Click() ' Move to the previous record dtaBooks.Recordset.MovePrevious ' Check if we're before the first record If dtaBooks.Recordset.BOF Then dtaBooks.Recordset.MoveFirst MsgBox "Beginning of records!", vbInformation End If End Sub Private Sub cmdLast_Click() ' Move to the last record dtaBooks.Recordset.MoveLast End Sub Private Sub cmdAdd_Click() ' Add a new record dtaBooks.Recordset.AddNew ' Clear fields for new data entry txtTitle.Text = "" txtAuthor.Text = "" txtISBN.Text = "" txtYear.Text = "" txtTitle.SetFocus End Sub Private Sub cmdSave_Click() ' Save the current record dtaBooks.Recordset.Update MsgBox "Record saved successfully!", vbInformation End Sub Private Sub cmdDelete_Click() ' Delete the current record with confirmation If MsgBox("Are you sure you want to delete this record?", vbYesNo + vbQuestion) = vbYes Then dtaBooks.Recordset.Delete dtaBooks.Recordset.MoveNext If dtaBooks.Recordset.EOF Then dtaBooks.Recordset.MoveLast End If End If End Sub
In VB.NET, the Data Control was replaced with more powerful ADO.NET components. While the concepts are similar, the implementation differs:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Load data from database Dim connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=books.mdb" Dim sql = "SELECT * FROM Books" Using conn As New OleDbConnection(connectionString) Dim adapter As New OleDbDataAdapter(sql, conn) Dim ds As New DataSet() adapter.Fill(ds, "Books") ' Set up binding source BindingSource1.DataSource = ds.Tables("Books") ' Bind controls txtTitle.DataBindings.Add("Text", BindingSource1, "Title") txtAuthor.DataBindings.Add("Text", BindingSource1, "Author") txtISBN.DataBindings.Add("Text", BindingSource1, "ISBN") txtYear.DataBindings.Add("Text", BindingSource1, "Year") End Using End Sub Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click ' Move to the first record BindingSource1.MoveFirst() End Sub Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click ' Move to the next record BindingSource1.MoveNext() End Sub Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click ' Add a new record BindingSource1.AddNew() End Sub Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click ' Save changes to the database Try BindingSource1.EndEdit() ' Update database here (code omitted for brevity) MessageBox.Show("Record saved successfully!") Catch ex As Exception MessageBox.Show("Error saving record: " & ex.Message) End Try End Sub
VB6: Uses ADO with connected architecture
VB.NET: Uses ADO.NET with disconnected architecture
VB6: Simple property binding to Data Control
VB.NET: Flexible and complex binding capabilities
VB6: Primarily Access, SQL Server via ODBC
VB.NET: Wide range via providers (SQL, Oracle, MySQL, etc.)
VB6: Limited security features
VB.NET: Integrated with .NET security model
Master database development with VB6 using practical examples and real-world projects
View Book