' Groceries3 Example ' Create database objects from code and display data in ' textboxes directly from the dataset (no data bindings). Imports System.Data.OleDb Public Class Form1 Inherits System.Windows.Forms.Form Private c As OleDbConnection Private sc As OleDbCommand Private da As OleDbDataAdapter Private ds As DataSet Private nRows As Integer Private currentRow As Integer #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents txtItemName As System.Windows.Forms.TextBox Friend WithEvents txtCode As System.Windows.Forms.TextBox Friend WithEvents txtPrice As System.Windows.Forms.TextBox Friend WithEvents txtQuantity As System.Windows.Forms.TextBox Friend WithEvents btnFirst As System.Windows.Forms.Button Friend WithEvents btnPrevious As System.Windows.Forms.Button Friend WithEvents btnNext As System.Windows.Forms.Button Friend WithEvents btnLast As System.Windows.Forms.Button Private Sub InitializeComponent() Me.txtItemName = New System.Windows.Forms.TextBox Me.txtCode = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.txtPrice = New System.Windows.Forms.TextBox Me.Label3 = New System.Windows.Forms.Label Me.txtQuantity = New System.Windows.Forms.TextBox Me.Label4 = New System.Windows.Forms.Label Me.btnFirst = New System.Windows.Forms.Button Me.btnPrevious = New System.Windows.Forms.Button Me.btnNext = New System.Windows.Forms.Button Me.btnLast = New System.Windows.Forms.Button Me.SuspendLayout() ' 'txtItemName ' Me.txtItemName.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtItemName.Location = New System.Drawing.Point(112, 56) Me.txtItemName.Name = "txtItemName" Me.txtItemName.ReadOnly = True Me.txtItemName.Size = New System.Drawing.Size(240, 26) Me.txtItemName.TabIndex = 0 Me.txtItemName.Text = "" ' 'txtCode ' Me.txtCode.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtCode.Location = New System.Drawing.Point(112, 96) Me.txtCode.Name = "txtCode" Me.txtCode.ReadOnly = True Me.txtCode.Size = New System.Drawing.Size(240, 26) Me.txtCode.TabIndex = 2 Me.txtCode.Text = "" ' 'Label1 ' Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(24, 56) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(88, 24) Me.Label1.TabIndex = 1 Me.Label1.Text = "ItemName" ' 'Label2 ' Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label2.Location = New System.Drawing.Point(64, 96) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(48, 24) Me.Label2.TabIndex = 3 Me.Label2.Text = "Code" ' 'txtPrice ' Me.txtPrice.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtPrice.Location = New System.Drawing.Point(112, 136) Me.txtPrice.Name = "txtPrice" Me.txtPrice.ReadOnly = True Me.txtPrice.Size = New System.Drawing.Size(240, 26) Me.txtPrice.TabIndex = 4 Me.txtPrice.Text = "" ' 'Label3 ' Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label3.Location = New System.Drawing.Point(64, 136) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(48, 24) Me.Label3.TabIndex = 5 Me.Label3.Text = "Price" ' 'txtQuantity ' Me.txtQuantity.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtQuantity.Location = New System.Drawing.Point(112, 176) Me.txtQuantity.Name = "txtQuantity" Me.txtQuantity.ReadOnly = True Me.txtQuantity.Size = New System.Drawing.Size(240, 26) Me.txtQuantity.TabIndex = 6 Me.txtQuantity.Text = "" ' 'Label4 ' Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label4.Location = New System.Drawing.Point(40, 176) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(72, 24) Me.Label4.TabIndex = 7 Me.Label4.Text = "Quantity" ' 'btnFirst ' Me.btnFirst.Location = New System.Drawing.Point(48, 16) Me.btnFirst.Name = "btnFirst" Me.btnFirst.Size = New System.Drawing.Size(64, 24) Me.btnFirst.TabIndex = 8 Me.btnFirst.Text = "First" ' 'btnPrevious ' Me.btnPrevious.Location = New System.Drawing.Point(128, 16) Me.btnPrevious.Name = "btnPrevious" Me.btnPrevious.Size = New System.Drawing.Size(64, 24) Me.btnPrevious.TabIndex = 9 Me.btnPrevious.Text = "Previous" ' 'btnNext ' Me.btnNext.Location = New System.Drawing.Point(208, 16) Me.btnNext.Name = "btnNext" Me.btnNext.Size = New System.Drawing.Size(64, 24) Me.btnNext.TabIndex = 10 Me.btnNext.Text = "Next" ' 'btnLast ' Me.btnLast.Location = New System.Drawing.Point(288, 16) Me.btnLast.Name = "btnLast" Me.btnLast.Size = New System.Drawing.Size(64, 24) Me.btnLast.TabIndex = 11 Me.btnLast.Text = "Last" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(376, 225) Me.Controls.Add(Me.btnLast) Me.Controls.Add(Me.btnNext) Me.Controls.Add(Me.btnPrevious) Me.Controls.Add(Me.btnFirst) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.txtQuantity) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.txtPrice) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.txtCode) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtItemName) Me.Name = "Form1" Me.Text = "GroceriesBinding Example" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Create and configure connection object. c = New OleDbConnection c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\Database\Groceries.mdb" ' Create and configure command object. sc = c.CreateCommand() sc.CommandType = CommandType.Text sc.CommandText = "SELECT * FROM GroceryList" ' Create and configure data adapter object. da = New OleDbDataAdapter da.SelectCommand = sc ' Create and configure dataset object. ds = New DataSet nRows = da.Fill(ds, "GroceryList") ' Initialize current row in table and display data. currentRow = 0 DisplayTextBoxValues() ' Unselect word in ItemName textbox. txtItemName.SelectionLength = 0 End Sub Private Sub DisplayTextBoxValues() ' Display data of current row in textboxes. ' Recall that column 0 is the primary key. txtItemName.Text = ds.Tables(0).Rows(currentRow).Item(1) txtCode.Text = ds.Tables(0).Rows(currentRow).Item(2) txtPrice.Text = ds.Tables(0).Rows(currentRow).Item(3) txtQuantity.Text = ds.Tables(0).Rows(currentRow).Item(4) End Sub Private Sub btnFirst_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnFirst.Click ' Move to first row and display data. currentRow = 0 DisplayTextBoxValues() End Sub Private Sub btnPrevious_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPrevious.Click ' Move to previous row and display data. If currentRow > 0 Then currentRow -= 1 End If DisplayTextBoxValues() End Sub Private Sub btnNext_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnNext.Click ' Move to next row and display data. If currentRow < nRows - 1 Then currentRow += 1 End If DisplayTextBoxValues() End Sub Private Sub btnLast_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnLast.Click ' Move to last row and display data. currentRow = nRows - 1 DisplayTextBoxValues() End Sub End Class