hi

I have an access database, which I connect it to the form

I have text boxes which displays info from the fields in the database.

I have created a "Next", and "Previous" button, which the user can move between the database info but i don't know the code for them can you plz help me

this is my code

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New OleDb.OleDbConnection
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\bb1.mdb"
        con.Open()
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String
        sql = "SELECT * FROM b1"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "b1")


        TextBox1.Text = ds.Tables("b1").Rows(0).Item(0)
        TextBox2.Text = ds.Tables("b1").Rows(0).Item(1)
        TextBox3.Text = ds.Tables("b1").Rows(0).Item(2)







        con.Close()

Hi,

The idea is, You need to override the adapter's fill method. Here is the the method info

adapter.Fill(Dataset, StartRecord, NumberofRecords, TableName)

Let me add the sample program here. This will navigate the records as you needed. But you need to add the validations such as last record, first record ... so on and try-catch exception handling and also may need some tweaks in increment/decrement navigation.

Public Class Form1

    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim recordIdx As Integer 'record index for navigation


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = c:\bb1.mdb"
        con.Open()
        Sql = "SELECT * FROM b1"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, recordIdx, 1, "b1")

        TextBox1.Text = ds.Tables("b1").Rows(0).Item(0)
        TextBox2.Text = ds.Tables("b1").Rows(0).Item(1)
        TextBox3.Text = ds.Tables("b1").Rows(0).Item(2)
        recordIdx += 1
        con.Close()
    End Sub

    Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
        If con.State <> ConnectionState.Open Then
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = c:\bb1.mdb"
            con.Open()
        End If

        sql = "SELECT * FROM b1"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, recordIdx, 1, "b1")

        TextBox1.Text = ds.Tables("b1").Rows(recordIdx).Item(0)
        TextBox2.Text = ds.Tables("b1").Rows(recordIdx).Item(1)
        TextBox3.Text = ds.Tables("b1").Rows(recordIdx).Item(2)
        recordIdx += 1
    End Sub

    Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevious.Click
        If con.State <> ConnectionState.Open Then
            con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = E:\Study\bb1.mdb"
            con.Open()
        End If
        sql = "SELECT * FROM b1"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, recordIdx, 1, "b1")

        TextBox1.Text = ds.Tables("b1").Rows(recordIdx).Item(0)
        TextBox2.Text = ds.Tables("b1").Rows(recordIdx).Item(1)
        TextBox3.Text = ds.Tables("b1").Rows(recordIdx).Item(2)
        recordIdx -= 1
    End Sub
End Class

Good luck.

Hi,
I made you a Small eg. Hope this will help you.

Private BS As New BindingSource

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ds As New DataSet

        ' Make the Connection
        Using con As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = c:\StockDetails.mdb")
            con.Open()
            Dim Sql = "SELECT * FROM Stocks"
            Dim da = New OleDb.OleDbDataAdapter(Sql, con)

            da.Fill(ds, "Stocks")

            ' Set the Binding Source
            BS.DataSource = ds.Tables("Stocks")

            con.Close()
        End Using

        TextBox1.DataBindings.Add("Text", BS, "StockItem")
        TextBox2.DataBindings.Add("Text", BS, "StockDesc")

    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        BS.MovePrevious()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        BS.MoveNext()
    End Sub

this is my code..can u hepl me out in coding furthur.

`Inline Code Example Here
Imports System.Data.OleDb

Public Class loan
    Dim con As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim dr As OleDbDataReader
    Private Sub loan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\ruchi\ruzzzzz\bm\WindowsApplication1\bank management.accdb"
        MsgBox("open")
        con.Open()

        cmd.CommandText = "select CID from customer"
        cmd.Connection = con
        dr = cmd.ExecuteReader
        While dr.Read
            ComboBox1.Items.Add(dr(0))
        End While
        dr.Close()
        con.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        con.Open()

        Dim s As String
        Dim n As Integer

        loanid.Text = " "
        ComboBox1.Text = ""

        DateTimePicker1.Text = ""
        DateTimePicker2.Text = ""
        intrstrate.Text = " "
        no_of_instlmnt.Text = " "
        instlmnt.Text = " "
        totalamnt.Text = " "

        txtname.Text = ""
        add.Text = ""
        DateTimePicker3.Text = ""
        phno.Text = ""


        Try
            cmd.CommandText = "select max(loanid) from LOAN"
            cmd.Connection = con
            dr = cmd.ExecuteReader
            dr.Read()
            s = dr(0)
            n = s.Substring(1)
            loanid.Text = "L" & (CInt(n.ToString) + 1).ToString
            dr.Close()
            con.Close()

        Catch ex As Exception
            loanid.Text = "L100"
            dr.Close()
            con.Close()

        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        con.Open()
        cmd.CommandText = "insert into LOAN values('" & loanid.Text & "','" & ComboBox1.Text & "','" & DateTimePicker1.Value & "','" & DateTimePicker2.Value & "','" & intrstrate.Text & "','" & no_of_instlmnt.Text & "','" & instlmnt.Text & "','" & totalamnt.Text & "','" & txtname.Text & "','" & add.Text & "','" & DateTimePicker3.Value & "','" & phno.Text & "')"
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        MsgBox("Record Save......")
        con.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles phno.TextChanged

    End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.