将文本框值插入到从两个组合框中选择的两个表中 [英] Insert textbox value into two tables selected from two comboboxes

查看:80
本文介绍了将文本框值插入到从两个组合框中选择的两个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以为我提供一些帮助
我有一个带有两个组合框,一个文本框和一个按钮的表单.我在ms access数据库中有多个具有相同结构col1和col2的表.
我想将文本框值插入到从combobox1选择的表的column1中,并插入到从combobox2选择的表的column2中. 例如,如果我从combobox1中选择一个table1,并从combobox2中选择了table2,然后单击按钮,则数据应同时插入table1和table2.
我可以将表格加载到组合框中,但是我不知道如何处理问题.有人可以告诉我(如果可能的话)我该怎么办

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

推荐答案

尝试一下并完成工作
让我们假设您有一个名为Name_Tab
的表 具有两个字段EmpId和EmpName

创建一个类似
的函数
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


在表单加载事件中将此MyFunction()调用
您将获得结果,就像在combobox1中一样,Empid将获得Filled
并在combobox2中,Empname将被填充
但是如果您想用
填充两个框 数据相同,而不仅仅是根据您的需要进行更改
实现这一点,让我知道结果


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


这篇关于将文本框值插入到从两个组合框中选择的两个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆