Thread: Question Populating combobox
View Single Post
  #4 (permalink)  
Old 10-10-2008, 5:18 PM
thirteentwenty thirteentwenty is offline
VB.NET Forum Enthusiast
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Oct 2008
Location: Honolulu
Posts: 61
Reputation: 6
thirteentwenty is on a distinguished programming path ahead
Default

Thank you once again rajhansh...

This is how I ended up doing it, now I'm sure that there are better ways, and this wasn't _exactly_ what I was looking for... but it works, and it works well (read: way better than what I had done before)...

Hopefully this thread can help other folks that are _extremely_ new to VB.NET get started with data access stuff in regards to comboboxs...

It's got some generic comments in it as I cant tell you _exactly_ what each line does, heck i cant even tell you what "Dim" means lol, but I kind of know whats going on...

This was compiled on Visual Studio 2005

What I did was create a Module called gbl (for global) can called the function to open and populate the comboboxes on load. I'll most likely put reference this in a splash screen as it takes a few seconds to load, and I think the load times will get longer as the database grows

Code:
Option Strict Off
Option Explicit On
Module gbl
    Dim con As OleDb.OleDbConnection
    Dim query As String
    Dim com As OleDb.OleDbCommand
    Dim da As OleDb.OleDbDataAdapter
    Dim dt As DataTable

    Public Function openDB()
' Open the database
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=TEST.mdb"
        con = New OleDb.OleDbConnection(connectionString)

        con.Open()

        'populates Reporters
        com = New OleDb.OleDbCommand("SELECT * FROM Reporters ORDER BY Name", con)
        da = New OleDb.OleDbDataAdapter
        da.SelectCommand = com
        dt = New DataTable
        da.Fill(dt)

        frm_invoices.cmb_inv_reporter.DataSource = dt
        frm_invoices.cmb_inv_reporter.DisplayMember = "Name"
        frm_invoices.cmb_inv_reporter.ValueMember = "ID"

        da.Dispose()
        da = Nothing
        com.Dispose()
        com = Nothing

        'populates Client Information
        com = New OleDb.OleDbCommand("SELECT * FROM Clients ORDER BY clients.[Client Name]", con)
        da = New OleDb.OleDbDataAdapter
        da.SelectCommand = com
        dt = New DataTable
        da.Fill(dt)

        frm_invoices.cmb_inv_client.DataSource = dt
        frm_invoices.cmb_inv_client.DisplayMember = "Client Name"
        frm_invoices.cmb_inv_client.ValueMember = "ID"

        frm_invoices.cmb_inv_cid.DataSource = dt
        frm_invoices.cmb_inv_cid.DisplayMember = "ID"
        frm_invoices.cmb_inv_cid.ValueMember = "ID"

        frm_invoices.cmb_inv_firmID.DataSource = dt
        frm_invoices.cmb_inv_firmID.DisplayMember = "FirmID"
        frm_invoices.cmb_inv_firmID.ValueMember = "ID"

        da.Dispose()
        da = Nothing
        com.Dispose()
        com = Nothing

        'populates Firms
        com = New OleDb.OleDbCommand("SELECT * FROM Firms ORDER BY [Firm Name]", con)
        da = New OleDb.OleDbDataAdapter
        da.SelectCommand = com
        dt = New DataTable
        da.Fill(dt)

        frm_invoices.cmb_inv_firm.DataSource = dt
        frm_invoices.cmb_inv_firm.DisplayMember = "Firm Name"
        frm_invoices.cmb_inv_firm.ValueMember = "ID"

        da.Dispose()
        da = Nothing
        com.Dispose()
        com = Nothing

        Return ""
    End Function
    Public Function DBstate()
        Select Case con.State
            Case 0
                frm_invoices.ToolStripStatusLabel1.Text = "Database Disconnected"
            Case 1
                frm_invoices.ToolStripStatusLabel1.Text = "Database Connected"
        End Select
        Return ""
    End Function
    Public Function checkDB()

        Select Case con.State
            Case 0
                MsgBox("Disconnected")
            Case 1
                MsgBox("Connected")
        End Select
        Return ""

    End Function

    Public Function closeDB()
        con.Dispose()
        con.Close()
        Return ""
    End Function
End Module

notice that I changed the variable names, I was just too lazy to re-type all the stuff that I had done...

** Fueled by crappy coffee and beef jerky I AM THE COPY AND PASTE KING!!!! **
Reply With Quote