Get data from database

Posted on 2005-05-12
Last Modified: 2010-04-23
I am having a hard time understanding how to access information from a database using VB.  What I need is somone to give me examples and explanations to do the following so I can use it as a base to learn more indepth stuff.  What I need is somone to show or explain how to do the following:

I have a database (microsoft access db .mdb) named mydb.mdb, it has the following fields (id,name,number)... all I want to do is select and display all the records where the ID = 5.
Question by:shawnlehner
    LVL 7

    Expert Comment

    use the wizard to create a form and display the data, (add new item, dataform wizard)
    once complete edit the data adapter(right click, configur data adapter)
    when you get to the query click "query Builder" , in the id column criteria add   =5
    ok to everything and run

    LVL 41

    Expert Comment

    I can recommend an article on beginning ADO.Net at

    Lots of explainations, comments, downloadable samples, etc
    LVL 62

    Accepted Solution

    Hi  shawnlehner;

    Here is some code I just put together you need to change 3 lines they are marked where changes need to be made.

     Imports System.Data.OleDb

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private myDataAdapter As OleDbDataAdapter
        Private myDataSet As DataSet
        Private myDataTable As DataTable

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            '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
                End If
            End If
        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 lbTableData As System.Windows.Forms.ListBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.lbTableData = New System.Windows.Forms.ListBox
            Me.lbTableData.Location = New System.Drawing.Point(32, 16)
            Me.lbTableData.Name = "lbTableData"
            Me.lbTableData.Size = New System.Drawing.Size(200, 134)
            Me.lbTableData.TabIndex = 0
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 273)
            Me.Name = "Form1"
            Me.Text = "Form1"

        End Sub

    #End Region

        Private Sub Form1_Load(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load

            ' Change the "data source=" to the path with filename of where your
            ' database is
            Dim connectionString As String = _
                "provider=Microsoft.Jet.OLEDB.4.0; " & _
                "data source=C:\temp\mydb.mdb"

            ' Change the word TableName with the actual table name
            Dim commandString As String = _
            "Select * from TableName Where id = 5"

            ' This creates the interface between the data set and the
            ' database it is connected to
            myDataAdapter = _
                New OleDbDataAdapter( _
                    commandString, connectionString)

            ' Create the data set that holds the local copy of the database
            ' you have selected
            myDataSet = New DataSet
            ' Fill the data set with the selected data
            ' Change TableName with the actual table name
            myDataAdapter.Fill(myDataSet, "TableName")

            ' A DataTable represents one table of in-memory relational data
            ' the one that is in the dataset
            myDataTable = myDataSet.Tables(0)
            Dim dataRow As DataRow
            ' Read each element of the myDataTable in to a list box
            For Each dataRow In myDataTable.Rows
                lbTableData.Items.Add((dataRow("id").ToString() & _
                    "    " & dataRow("name").ToString() & dataRow("number").ToString()))
            Next dataRow

        End Sub
    End Class

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now