Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

VS 2012 Search for and move a DataRow between two tables in an Access database.

$
0
0
I'm a beginner, and I'm trying to teach myself vb.net as I undertake an admittedly ambitious project. I've searched the web and found many examples of code, but none that match my exact situation.

I have an MS Access database, containing two tables. The first table contains 6 columns of data labeled "ID", "Threat", "Type", "Span", Length", and "Speed". The table is populated with data for variety of aircraft. Data from the "Type" column is displayed to the user for selection in a Listbox.

The second table is similar, but has one additional checkbox column. Initially, this table is empty, and I want the user to be able to populate this database with no more than 10 rows of data from the first table. Data for all columns except ID is displayed to the user in a DataGridView.

I want the user to select an aircraft from the Listbox, press a button, and cause the associated data for that aircraft appear in the DataGridView.

After much trial and error, I cannot get the DataRow to transfer. Depending on what I mess with, I get varying error messages. Please take a look at my terrible code and tell me where I'm going wrong:

Code:

    Private Sub btnTransferThreat_Click(sender As Object, e As EventArgs) Handles btnTransferThreat.Click

        Dim Threat As String = AaThreatMaster.SelectedValue

        Dim conAAThreats As New OleDb.OleDbConnection
        conAAThreats.ConnectionString = "PROVIDER=Microsoft.jet.OLEDB.4.0;Data Source=C:\Users\NL\My Databases\AaThreats.mdb;"

        'Create the adapters
        Dim adapterAAThreatsFxd As New OleDb.OleDbDataAdapter("SELECT * FROM aaThreatMaster WHERE Threat = Threat", conAAThreats)
        Dim adapterAAThreatsRty As New OleDb.OleDbDataAdapter("SELECT * FROM aaThreatMaster WHERE Threat = Threat", conAAThreats)
        Dim adapterAAThreatStored As New OleDb.OleDbDataAdapter("SELECT THREAT,SPAN,LENGTH,SPEED FROM aaThreatStored", conAAThreats)

        'Create the datasets
        Dim dataAAThreatsFxd As New DataSet
        Dim dataAAThreatsRty As New DataSet
        Dim dataAAThreatStored As New DataSet

        'Populate the datasets with database info
        adapterAAThreatsFxd.Fill(dataAAThreatsFxd, "AAThreats")
        adapterAAThreatsRty.Fill(dataAAThreatsRty, "AAThreats")
        adapterAAThreatStored.Fill(dataAAThreatStored, "AAThreats")

        Dim SelectedTable As DataTable = dataAAThreatsFxd.Tables("AAThreats")
        Dim Selected As Data.DataRow() = SelectedTable.Select("Threat=" & Threat)

        'MsgBox(Selected.ToString)

        Dim newThreatRow As DataRow = dataAAThreatStored.Tables("AAThreats").NewRow

        newThreatRow = Selected()

        dataAAThreatStored.Tables("AAThreats").Rows.Add(newThreatRow)

    End Sub

Thank you in advance!

Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>