' Groceries5 Example ' Select items to display in a multiline textbox ' according to whether the items are on sale. 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 #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 txtDisplay As System.Windows.Forms.TextBox Friend WithEvents btnAll As System.Windows.Forms.Button Friend WithEvents btnOnSale As System.Windows.Forms.Button Friend WithEvents btnNotOnSale As System.Windows.Forms.Button Private Sub InitializeComponent() Me.txtDisplay = New System.Windows.Forms.TextBox Me.btnAll = New System.Windows.Forms.Button Me.btnOnSale = New System.Windows.Forms.Button Me.btnNotOnSale = New System.Windows.Forms.Button Me.SuspendLayout() ' 'txtDisplay ' Me.txtDisplay.Font = New System.Drawing.Font("Courier New", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtDisplay.Location = New System.Drawing.Point(8, 40) Me.txtDisplay.Multiline = True Me.txtDisplay.Name = "txtDisplay" Me.txtDisplay.Size = New System.Drawing.Size(328, 272) Me.txtDisplay.TabIndex = 0 Me.txtDisplay.Text = "" ' 'btnAll ' Me.btnAll.Location = New System.Drawing.Point(8, 8) Me.btnAll.Name = "btnAll" Me.btnAll.Size = New System.Drawing.Size(88, 24) Me.btnAll.TabIndex = 1 Me.btnAll.Text = "Select All" ' 'btnOnSale ' Me.btnOnSale.Location = New System.Drawing.Point(112, 8) Me.btnOnSale.Name = "btnOnSale" Me.btnOnSale.Size = New System.Drawing.Size(96, 24) Me.btnOnSale.TabIndex = 2 Me.btnOnSale.Text = "Select On Sale" ' 'btnNotOnSale ' Me.btnNotOnSale.Location = New System.Drawing.Point(224, 8) Me.btnNotOnSale.Name = "btnNotOnSale" Me.btnNotOnSale.Size = New System.Drawing.Size(112, 24) Me.btnNotOnSale.TabIndex = 3 Me.btnNotOnSale.Text = "Select Not On Sale" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(344, 318) Me.Controls.Add(Me.btnNotOnSale) Me.Controls.Add(Me.btnOnSale) Me.Controls.Add(Me.btnAll) Me.Controls.Add(Me.txtDisplay) 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") ' Display all of the rows in the textbox. DisplayRows() End Sub Private Sub DisplayRows() ' Refill dataset. ds.Clear( ) nRows = da.Fill(ds, "GroceryList") Dim output As String = " Item Name Code Price N" & _ ControlChars.NewLine & "============ ==== ===== ==" & _ ControlChars.NewLine Dim i As Integer For i = 0 To nRows - 1 output &= String.Format("{0,-15}{1,6}{2,6:$#0.00}{3,4}", _ ds.Tables(0).Rows(i).Item(1), _ ds.Tables(0).Rows(i).Item(2), _ ds.Tables(0).Rows(i).Item(3), _ ds.Tables(0).Rows(i).Item(4)) + ControlChars.NewLine Next txtDisplay.Text = output txtDisplay.SelectionLength = 0 End Sub Private Sub btnAll_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnAll.Click sc.CommandText = "SELECT * FROM GroceryList" DisplayRows() End Sub Private Sub btnOnSale_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOnSale.Click sc.CommandText = "SELECT * FROM GroceryList WHERE IsOnSale=True" DisplayRows() End Sub Private Sub btnNotOnSale_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnNotOnSale.Click sc.CommandText = "SELECT * FROM GroceryList WHERE IsOnSale=False" DisplayRows() End Sub End Class