' PhoneBookDB Example ' Input a name, then lookup the phone number in a database. Imports System.Data.OleDb Public Class Form1 Inherits System.Windows.Forms.Form #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 btnSubmit As System.Windows.Forms.Button Friend WithEvents txtName As System.Windows.Forms.TextBox Friend WithEvents txtNumber As System.Windows.Forms.TextBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Private Sub InitializeComponent() Me.btnSubmit = New System.Windows.Forms.Button Me.txtName = New System.Windows.Forms.TextBox Me.txtNumber = New System.Windows.Forms.TextBox Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'btnSubmit ' Me.btnSubmit.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.btnSubmit.Location = New System.Drawing.Point(16, 152) Me.btnSubmit.Name = "btnSubmit" Me.btnSubmit.Size = New System.Drawing.Size(208, 32) Me.btnSubmit.TabIndex = 0 Me.btnSubmit.Text = "Get Phone Number" ' 'txtName ' Me.txtName.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtName.Location = New System.Drawing.Point(16, 16) Me.txtName.Name = "txtName" Me.txtName.Size = New System.Drawing.Size(208, 26) Me.txtName.TabIndex = 1 Me.txtName.Text = "" ' 'txtNumber ' Me.txtNumber.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtNumber.Location = New System.Drawing.Point(16, 80) Me.txtNumber.Name = "txtNumber" Me.txtNumber.Size = New System.Drawing.Size(208, 26) Me.txtNumber.TabIndex = 2 Me.txtNumber.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(88, 48) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(64, 24) Me.Label1.TabIndex = 3 Me.Label1.Text = "Name" ' '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(56, 120) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(128, 24) Me.Label2.TabIndex = 4 Me.Label2.Text = "Phone Number" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(248, 198) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtNumber) Me.Controls.Add(Me.txtName) Me.Controls.Add(Me.btnSubmit) Me.Name = "Form1" Me.Text = "PhoneNumberDB Example" Me.ResumeLayout(False) End Sub #End Region Dim c As New OleDbConnection Dim sc As OleDbCommand Dim da As New OleDbDataAdapter Dim ds As New DataSet Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=c:\Database\PhoneNumbers.mdb" c.Open() sc = c.CreateCommand() sc.CommandType = CommandType.Text da.SelectCommand = sc End Sub Private Sub btnSubmit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSubmit.Click Dim n As Integer Dim sql As String = "SELECT PhoneNumber FROM PhoneNumbers WHERE " & _ "Name = '" & txtName.Text & "'" sc.CommandText = sql n = da.Fill(ds, "PhoneNumbers") txtNumber.Text = ds.Tables(0).Rows(0).Item(0) End Sub End Class