Ashwin975 0 Newbie Poster

Hi All,

I have 7 tables in my access database. i wrote a code to select the data table using the combobox and to display the selected table in the datagridview.

Now i need to add,edit or update, delete the data into the selected tables. In which the text boxes are common to the all the tables, for ex: if i select the table "Capacitor" in the combobox, whatever the data i entered into the textboxes will go to the "capacitor" tables, likewise other tables. Please anyone help me to resolve this code, i am struck in it for long time, as i am new to vb. Below is my code.

Public Class Form1

'To get the tables in the combobox

Dim cnn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'Mentor_DX_Library_DatabaseDataSet.Capacitor' table. You can move, or remove it, as needed.
    Me.CapacitorTableAdapter.Fill(Me.Mentor_DX_Library_DatabaseDataSet.Capacitor)

    cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\ashwin_seshasrinivas\Desktop\odbc\Edited database\Mentor_DX_Library_Database.accdb;Persist Security Info=False;")
    cnn.Open()
    Me.ComboBox1.DisplayMember = "TABLE_NAME"
    Dim restrictions() As String = New String(3) {}
    restrictions(3) = "Table"
    Me.ComboBox1.DataSource = cnn.GetSchema("Tables", restrictions)
    BindGrid()

End Sub

'To display the selected table in the datagridview

Private Sub BindGrid()

    Dim cnn As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection()
    cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\ashwin_seshasrinivas\Desktop\odbc\Edited database\Mentor_DX_Library_Database.accdb;Persist Security Info=False;")
    Dim con As New OleDb.OleDbConnection
    Dim sSql As String = "Select * From [" & ComboBox1.Text & " ]"
    Dim cmd As New OleDb.OleDbCommand(sSql, cnn)
    cmd.CommandType = CommandType.Text
    Dim sda As New OleDb.OleDbDataAdapter(cmd)
    Dim dt As New DataTable()
    sda.Fill(dt)
    DataGridView1.DataSource = dt
End Sub

' Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'BindGrid()
'End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    BindGrid()
    ' If ComboBox1.Text = "Capacitor" Then
    ' End If
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

    Me.txtdes.Text = ""
    Me.txtrohs.Text = ""
    Me.txtmanf.Text = ""
    Me.txtmanfpn.Text = ""
    Me.txtagile.Text = ""
    Me.txttype.Text = ""
    Me.txtpack.Text = ""
    Me.txtvalue.Text = ""
    Me.txtvoltage.Text = ""
    Me.txttolerance.Text = ""
    Me.txttemp.Text = ""
    Me.txtpartnum.Text = ""
    Me.txtcell.Text = ""
    Me.txtsym.Text = ""
    Me.txtagile.Tag = ""

End Sub

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

    If ComboBox1.Text = "Capacitor" Then
        CapacitorBindingsource.AddNew()
    End If

End Sub

Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click

End Sub

Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
    If ComboBox1.Text = "Capacitor" Then
        CapacitorBindingsource.Removecurrent()
    End If
End Sub

End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.