' Project Groceries7 ' Construct SQL statement based on the Code textbox and display ' other fields corresponding to that Code value. ' This is the Groceries4 Example WITHOUT data binding. 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 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 btnSelect 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.btnSelect = 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, 104) 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, 8) Me.txtCode.Name = "txtCode" 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, 104) 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, 8) 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, 144) 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, 144) 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, 184) 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, 184) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(72, 24) Me.Label4.TabIndex = 7 Me.Label4.Text = "Quantity" ' 'btnSelect ' Me.btnSelect.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnSelect.Location = New System.Drawing.Point(112, 48) Me.btnSelect.Name = "btnSelect" Me.btnSelect.Size = New System.Drawing.Size(240, 32) Me.btnSelect.TabIndex = 8 Me.btnSelect.Text = "Select By Code" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(376, 233) Me.Controls.Add(Me.btnSelect) 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 = "Groceries7 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 WHERE Code = 99999" ' 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") End Sub Private Sub btnSelect_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSelect.Click ' Construct SQL statement based on Code in textbox. sc.CommandText = "SELECT * FROM GroceryList WHERE Code = " & txtCode.Text ' Clear previous value from Dataset object. ds.Clear() ' Fill Dataset object with new data. nRows = da.Fill(ds, "GroceryList") ' Display data in textboxes. If nRows > 0 Then txtItemName.Text = ds.Tables(0).Rows(0).Item(1) txtPrice.Text = ds.Tables(0).Rows(0).Item(3) txtQuantity.Text = ds.Tables(0).Rows(0).Item(4) End If End Sub End Class