locked
Populate Listbox with Access Database?

    Question

  • Is there a way in which I can populate my listbox with an Access (.mdb) database and search for values in it. Also, can I move selected values back & forth from that listbox to another listbox (or textbox). Is this possible using Visual Studio, VB.Net??

    Thanks! Any help is much appreciated.

    Friday, July 08, 2011 4:34 AM

Answers

  • that's because you are setting your connection, but you also need to use a SELECT query to pull the data from the database

    the code above (from the other thread) is just an example of a dummy datatable created to show the idea. You need to populate your datatable with data from your Access database, then set the ListBox datasource to your datatable, and the DisplayMember to the column from your datatable (whatever your column name is)

    look at some of the links in the Related Topics pane on the right side of this window. There are 4 or 5  having to do specifically with Populating a ListBox from an Access database. Look at some of those to see how to pull the data from the database.

    Sunday, July 10, 2011 6:14 AM

All replies

  • yes, yes and yes

    you can populate the ListBox with values from a column in a datatable. See this recent thread for an example (also look to the right in the Related Topics pane):

    http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/61438358-6e19-48b2-a64c-3e0613beda87

    the method of searching depends on a number of factors. You can search the Forums or Google something like "VB.NET search listbox for items"

    and you can move items from one ListBox into another ListBox using the ListBox SelectedItems collection and ListBox Items.Add method

     

     

    Friday, July 08, 2011 4:49 AM
  • Thanks Jwavila,

    I'm trying it out now. Please keep an eye on this thread, I would need some expert advice incase I'm stuck.

    Thanks!

    Friday, July 08, 2011 5:23 AM
  • Hey Jwavila...

    I went thru those links you provided.. very helpful.. I implemented it but I'm still unable to get it displayed.

    Here is the code, can u tell me where am I going wrong?

    -----------------------------------------

    Imports

     

    System.Data.OleDb

    Public

     

    Class

    Form1

     

    Dim dbConnection As

    OleDbConnection

     

    Dim dbCommand As

    OleDbCommand

     

    Dim strInsert As

    String

     

    Dim dbDataAdapter As

    OleDbDataAdapter

     

    Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" &

    "Data Source =atg.mdb"

     

    Dim dtATG As

    DataTable

     

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

    .Load

     

    Dim dtATG As New

    DataTable

    dtATG.Columns.Add(

    "Col1", GetType(String

    ))

     

    For i As Integer = 1 To

    10

    dtATG.Rows.Add(i,

    "Row "

    & i.ToString)

     

    Next

    ListBox1.DisplayMember =

    "Col1"

    ListBox1.ValueMember =

    "Col1"

    ListBox1.DataSource = dtATG.DefaultView

     

    AddHandler ListBox1.SelectedIndexChanged, AddressOf

    ListBox1_SelectedIndexChanged

     

    End

    Sub

     

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs

    )

     

    MessageBox

    .Show(ListBox1.SelectedValue.ToString)

     

    End

    Sub

     

    End

     

    Class

    Friday, July 08, 2011 6:59 AM
  • Hey Jwavila,

    I got it running partially. But the Listbox shows only numbers from 1 - 10 as results and not the data from the Access database.

    Imports System.Data.OleDb
    Public Class Form1
      Dim dbConnection As OleDbConnection
      Dim dbCommand As OleDbCommand
      Dim strInsert As String
      Dim dbDataAdapter As OleDbDataAdapter
      Dim ConnectString As String = "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =atg.mdb"
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dtATG As New DataTable
        dtATG.Columns.Add("Col1", GetType(Integer))
        For i As Integer = 1 To 10
          dtATG.Rows.Add(i)
        Next
        ListBox1.DisplayMember = "Col1"
        ListBox1.ValueMember = "Col1"
        ListBox1.DataSource = dtATG
      End Sub
    End Class

    I have set the oledb connection etc correctly..

    Friday, July 08, 2011 12:02 PM
  • that's because you are setting your connection, but you also need to use a SELECT query to pull the data from the database

    the code above (from the other thread) is just an example of a dummy datatable created to show the idea. You need to populate your datatable with data from your Access database, then set the ListBox datasource to your datatable, and the DisplayMember to the column from your datatable (whatever your column name is)

    look at some of the links in the Related Topics pane on the right side of this window. There are 4 or 5  having to do specifically with Populating a ListBox from an Access database. Look at some of those to see how to pull the data from the database.

    Sunday, July 10, 2011 6:14 AM
  • Hi Nitn,

    Any progress on this issue?

    Please feel free to ask if you have not get it resolved.

     

    Best Regards,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, July 14, 2011 7:00 AM
    Moderator