1

I'm seeking help on how to return more than one row in a query. For example if I want the entire contents of a column in access, I want to return all rows to a ListBox.

Imports System.Data.OleDb

Public Class Form1

    Dim theConnectionString As New OleDbConnection

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        txtSQL.Clear()
        theResults.Items.Clear()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        theConnectionString.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Marc Wilson\Documents\FiddleFuckDB.accdb"
        theConnectionString.Open()
        Dim theDataSet As New DataSet
        Dim theDataTable As New DataTable
        theDataSet.Tables.Add(theDataTable)
        Dim theDataAdapter As New OleDbDataAdapter

        Dim theSQLStatement As String
        Dim theDBCommand As New OleDbCommand
        theSQLStatement = txtSQL.Text


        Try
            theDataAdapter = New OleDbDataAdapter("SELECT [City] FROM PI", theConnectionString)
            theDataAdapter.Fill(theDataTable)
            Dim theRowCount As Integer = theDataTable.Rows.Count
            Dim theItemCount As Integer = theDataTable.Columns.Count
            MessageBox.Show("Row Count: " & theRowCount & vbNewLine & "Item Count: " & theItemCount, "Your Results...")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

        Try
            theDBCommand = New OleDbCommand(theSQLStatement, theConnectionString)
            theResults.Items.Add(theDataTable.Rows(0).Item(0))
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        theConnectionString.Close()
    End Sub
End Class

The SQL Statement I'm using is SELECT [City] FROM PI (I have 3 rows of data and 4 different columns) I believe my mistake resides in this portion theResults.Items.Add(theDataTable.Rows(0).Item(0))

0

2 Answers 2

1

You haven't shown the sql statement from txtSQL.Text that you are attempting to use.

A simple SELECT statement will retrieve all rows from a table, unless a WHERE clause is used to restrict the number of rows returned.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry. The SQL Statement I'm using is "SELECT [City] FROM PI" (I have 3 rows of data and 4 different columns)
0

Try like this ........

Dim x as Integer

Try

   theDataAdapter = New OleDbDataAdapter("SELECT [City] FROM PI", theConnectionString)
   theDataAdapter.Fill(theDataTable)

   For x = 0 to theDataTable.Rows.Count -1
      theResults.Items.Add(theDataTable.Rows(x).Item(0))
   Next

Catch ex As Exception

   MessageBox.Show(ex.ToString)

End Try

2 Comments

Tried it. It just returns Row 1 three times.
@mwilson : have you try this ? .. any comment ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.