Show all Records and Fields in Access-Db in a Textbox

Wilfred Eghenedji 326 Reputation points
2022-05-17T06:57:00.387+00:00

Am creating a bible reference application, using Microsoft Office Access as Database. The point is, am able to code retrieving individual bible verse through its corresponding chapter, and show it on a textbox, but now wants to show all the records and fields in the access-db in a textbox, so that the bible can also be accessed as a whole in textbox. Is this possible? I have attempted, using the following:

Dim command As OleDbCommand = New OleDbCommand()
connection.Open()
command.Connection = connection
command.CommandText = "Select * from NewTestament
Dim dr As OleDbDataReader = command.ExecuteReader()

While dr.Read()
   LblBibleVerses2.Text = dr("*").ToString()
End While

dr.Close()
connection.Close()

My fields in access db are: BibleReference and BibleReadings. Table name is 'NewTestament'

Thank you.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-05-17T07:18:41.383+00:00

    Try something like this:

    . . .
    command.CommandText = "Select BibleReadings from NewTestament order by BibleReference"
    Dim dr As OleDbDataReader = command.ExecuteReader()
    
    While dr.Read()
        textbox1.AppendText( dr("BibleReadings").ToString( ) )
        textbox1.AppendText( Environment.NewLine )
    End While
    . . .
    

    It assumes that BibleReference can be used in 'order by' to order the verses.


1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2022-05-17T18:24:13.857+00:00

    I'm going to recommend a different approach, load data into a DataTable as per below.

    Public Class DatabaseOperations
        Public Function LoadNewTestament() As DataTable
    
            Using cn As New OleDbConnection("TODO")
                Using cmd As New OleDbCommand With {.Connection = cn}
                    cmd.CommandText = <SQL>
                        SELECT 
                            BibleReference, 
                            BibleReadings
                        FROM 
                            NewTestament
                        </SQL>.Value
    
                    Dim dt As New DataTable
                cn.Open()
                dt.Load(cmd.ExecuteReader)
                    Return dt
                End Using
            End Using
        End Function
    
    End Class
    

    In your form, create a form level BindingSource, set the data source for the BindingSource to the DataTable above. Next create a ListBox or ComboBox, assign it's DataSource to the BindingList and DisplayMember to BibleReference. Next create a TextBox and use SomeTextBox.DataBinding.Add(...

    Unsure if BibleReference or BibleReadings is the main property.

    Now when you traverse items in the ListBox the TextBox shows the data for the selected ComboBox or ListBox item.