Solved

Populate textbox with combobox selection

Posted on 2009-05-14
2
995 Views
Last Modified: 2013-11-26
I am trying to create a database program in Visual Basic 2008 using Access Database.
I have 2 tables, Invoice and Customer.  Customer Table is bound to a combobox. I would like to populate three textboxes on my invoice form with the content of the combobox.  i.e. The user selects a customer from the combobox and the customers Name, Address and City are put in the textboxes on an invoice form.
Imports System.Data
Imports System.Data.OleDb
 
Public Class Form1
    Dim DbConnection As OleDbConnection
    Dim InvoiceCommand As OleDbCommand
    Dim InvoiceAdapter As OleDbDataAdapter
    Dim InvoiceTable As DataTable
    Dim InvoiceManager As CurrencyManager
    Dim CustomerCommand As OleDbCommand
    Dim CustomerAdapter As OleDbDataAdapter
    Dim CustomerTable As DataTable
    Dim CustomerManager As CurrencyManager
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'connect to database
        DbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = c:\Database2.mdb")
        DbConnection.Open()
 
        'establish command object
        InvoiceCommand = New OleDbCommand("Select * from tblInvoice ORDER BY CustomerName", DbConnection)
 
        'establish data adapter/data table
        InvoiceAdapter = New OleDbDataAdapter()
        InvoiceAdapter.SelectCommand = InvoiceCommand
        InvoiceTable = New DataTable()
        InvoiceAdapter.Fill(InvoiceTable)
 
        'bind controls to data table
        txtName.DataBindings.Add("Text", InvoiceTable, "CustomerName")
        txtAddress.DataBindings.Add("Text", InvoiceTable, "Address")
        txtCity.DataBindings.Add("Text", InvoiceTable, "City")
 
 
        'establish customer table/combo box to pick customer
        CustomerCommand = New OleDbCommand("Select * from TblCustomer ORDER BY CustomerID", DbConnection)
        CustomerAdapter = New OleDbDataAdapter()
        CustomerAdapter.SelectCommand = CustomerCommand
        CustomerTable = New DataTable()
        CustomerAdapter.Fill(CustomerTable)
 
        ComboBox1.DataSource = CustomerTable
        ComboBox1.DisplayMember = "CustomerID"
        ComboBox1.ValueMember = "CustomerID"
        ' ComboBox1.DataBindings.Add("SelectedValue", InvoiceTable, "CustomerName")
 
    End Sub
 
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        txtName.Text = ComboBox1.SelectedText
        
    End Sub

Open in new window

0
Comment
Question by:williamaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 500 total points
ID: 24391130
create a strycture to hold the name, address and city. Load the structure to the combo box. On event SelectIndexChanged write the data to your textboxes. I didn't check the code below, wrote it in notepad, but it should get you real close.
    'Put this structure in a Module
    Public Structure CustData
        Dim Name As String
        Dim Address As String
        Dim City As String
 
        Public Sub New(ByVal _Name As String, ByVal _Address As String, ByVal _City As String)
            Name = _Name
            Address = _Address
            City = _City
        End Sub
 
        Public Overrides Function ToString() As String
            Return Me.Name
        End Function
    End Structure
 
    Public Sub LoadComboBox()
 
        Dim oCn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = c:\Database2.mdb")
        Dim oCmd As New OleDb.OleDbCommand("Select * from tblInvoice ORDER BY CustomerName", oCn)
 
        oCn.Open()
        Dim oDr As OleDb.OleDbDataReader = oCmd.ExecuteReader()
 
        While oDr.Read()
            Dim oExtraData As Object = Nothing
            yourComboBox.Items.Add(New CustData(oDr("Name"), oDr("Address"), oDr("City")))
        End While
        oDr.Close()
        oCn.Close()
    End Sub
 
    Private Sub yourComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yourComboBox.SelectedIndexChanged
        txtName.text = yourComboBox.SelectedItem.Name
        txtAddress.text = yourComboBox.SelectedItem.Address
        txtCity.text = yourComboBox.SelectedItem.City
    End Sub

Open in new window

0
 

Author Closing Comment

by:williamaa
ID: 31656983
Excellent.  Thank you very much have searched for 3 days for the correct answer.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

735 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