Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I was hoping someone could offer me some help please
I have a form with two comboboxes ,one text box and a button .I have multiple tables in ms access database with same structure , col1 and col2 .
I want to insert text box value into column1 in the table selected from combobox1 and into column2 in table selected from combobox2
such as if i select a table1 from combobox1 and table2 from combobox2 and click on the button, data should be inserted into both table1 and table2.
I can load tables into combobox but i dont know how to handle the issue.can someone tell me (if it is possible) how can i do it
Private con As New OleDbConnection("  Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\dtb.accdb;Jet OLEDB:Database Password=123456  ")
    Private adapter As New OleDbDataAdapter(String.Empty, Me.con)
    Private data As DataTable
    Private Sub Form1_Load(ByVal sender As Object, _
                       ByVal e As EventArgs) Handles MyBase.Load
        con.Open()
        Me.ComboBox1.DisplayMember = "TABLE_NAME"
        Me.ComboBox1.ValueMember = "TABLE_NAME"
        Me.ComboBox1.DataSource = Me.con.GetSchema("TABLES")
       
        Me.ComboBox2.DisplayMember = "TABLE_NAME"
        Me.ComboBox2.ValueMember = "TABLE_NAME"
        Me.ComboBox2.DataSource = Me.con.GetSchema("TABLES")
        con.Close()
       End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
                                           ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If Me.ComboBox1.SelectedItem IsNot Nothing Then
            Me.data = New DataTable
            Me.adapter.SelectCommand.CommandText = String.Format("SELECT * FROM [{0}]", Me.ComboBox1.SelectedValue)
           
           
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, _
                           ByVal e As EventArgs) Handles Button1.Click
        If Me.data IsNot Nothing Then
            Dim builder As New OleDbCommandBuilder(Me.adapter)
            Me.adapter.Update(Me.data)
        End If
    End Sub
Posted
Comments
walterhevedeich 3-May-11 0:45am    
You can load tables into combobox but you dont know how to handle the issue. Whats the issue?
josef01 4-May-11 2:00am    
Thank you for reply
The problem i am running into is that i need to use same code in multiple comboboxes, combobox will have to be named different but the code to perform task will be same .i am new to .net,i know there might be way to do it but i dont know where to start looking .
here i am using datagridview and combobox
<pre>
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If Me.ComboBox1.SelectedItem IsNot Nothing Then
Me.data = New DataTable
Me.adapter.SelectCommand.CommandText = String.Format("SELECT * FROM [{0}]", Me.ComboBox1.SelectedValue)
Me.adapter.Fill(data)

Me.DataGridView1.DataSource = Nothing
Me.DataGridView1.Columns.Clear()
Me.DataGridView1.DataSource = Me.data
End If
End Sub</pre>

I am trying to use the same code in another set of combobox
<pre>
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If Me.ComboBox2.SelectedItem IsNot Nothing Then
Me.data = New DataTable
Me.adapter.SelectCommand.CommandText = String.Format(&quot;SELECT * FROM [{0}]&quot;, Me.ComboBox2.SelectedValue)
Me.adapter.Fill(data)

Me.DataGridView2.DataSource = Nothing
Me.DataGridView2.Columns.Clear()
Me.DataGridView2.DataSource = Me.data
End If
End Sub</pre>

Sandeep Mewara 3-May-11 0:54am    
What issue?
josef01 4-May-11 2:00am    
Thank you for reply
The problem i am running into is that i need to use same code in multiple comboboxes, combobox will have to be named different but the code to perform task will be same .i am new to .net,i know there might be way to do it but i dont know where to start looking .
here i am using datagridview and combobox
<pre>
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If Me.ComboBox1.SelectedItem IsNot Nothing Then
Me.data = New DataTable
Me.adapter.SelectCommand.CommandText = String.Format("SELECT * FROM [{0}]", Me.ComboBox1.SelectedValue)
Me.adapter.Fill(data)

Me.DataGridView1.DataSource = Nothing
Me.DataGridView1.Columns.Clear()
Me.DataGridView1.DataSource = Me.data
End If
End Sub</pre>

I am trying to use the same code in another set of combobox
<pre>
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If Me.ComboBox2.SelectedItem IsNot Nothing Then
Me.data = New DataTable
Me.adapter.SelectCommand.CommandText = String.Format(&quot;SELECT * FROM [{0}]&quot;, Me.ComboBox2.SelectedValue)
Me.adapter.Fill(data)

Me.DataGridView2.DataSource = Nothing
Me.DataGridView2.Columns.Clear()
Me.DataGridView2.DataSource = Me.data
End If
End Sub</pre>


1 solution

Try This And Got your Work Done
Let us Suppose You have a table named Name_Tab
having Two Fields EmpId and EmpName

Create A function Like
Public Sub MyFunction()
       
        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand("Select * From Name_tab", myCon.con())
        cmd.CommandType = CommandType.Text
        Dim dacmd As New SqlClient.SqlDataAdapter(cmd)
        
        Dim dt As New DataTable
        dacmd.Fill(dt)

        For i As Integer = 0 To dt.Rows.Count - 1
            ComboBox1.Items.Add(dt.Rows(i).Item(0))
            ComboBox2.Items.Add(dt.Rows(i).Item(1))

        Next
    End Sub

Call this MyFunction () on your Form Load Event
You will get the Result as in combobox1 Empid will get Filled
and in combobox2 Empname will get Filled
But If you Want to fill both the boxes With
Same Data than Just Change it as Per Your Need
Implement this and let me know the Result
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900