Home > Software design >  Coding navigation buttons using vb.net 2012
Coding navigation buttons using vb.net 2012

Time:10-18

My main goal is to code navigation buttons to control records of a vb.net project connected with Microsoft Access Database. The four buttons included are First record, Last record, Next record and Previous record. The errors showing say "ShowData" and "CurrentRow" are not declared.

enter image description here

CurrentRow shows the following error correction options:

enter image description here

ShowData shows the following error correction options:

enter image description here

 'to navigate to first record
Private Sub btn_first_Click(sender As Object, e As EventArgs) Handles btn_first.Click
    CurrentRow = 0
    ShowData(CurrentRow)

End Sub
'to navigate to last record
Private Sub btn_last_Click(sender As Object, e As EventArgs) Handles btn_last.Click
    CurrentRow = Dst.Tables("Purchases_file").Rows.Count - 1
    ShowData(CurrentRow)

End Sub
'to navigate to previous record
Private Sub btn_previous_Click(sender As Object, e As EventArgs) Handles btn_previous.Click
    If CurrentRow <> 0 Then
        CurrentRow -= 1
        ShowData(CurrentRow)
    Else
        MsgBox("First record is reached!", MsgBoxStyle.Exclamation)
    End If
End Sub
'to navigate to next record
Private Sub btn_next_Click(sender As Object, e As EventArgs) Handles btn_next.Click
    If CurrentRow = Dst.Tables("Purchases_file").Rows.Count - 1 Then
        MsgBox("Last record is reached!", MsgBoxStyle.Exclamation)
    Else
        CurrentRow  = 1
        ShowData(CurrentRow)
    End If
End Sub

CodePudding user response:

According to the error you get you did not declare CurrentRow before using it in the methods. Put

Private CurrentRow as Integer

in your class. ShowData calls the method to retrieve the data from the database. It should be something like this:

Private Sub ShowData(byval row as integer)
'code to get the data in the database
End Function

If the variable and method exist in another class of the project you need to reference them via their class. For example:

ClassName.CurrentRow
ClassName.ShowData(CurrentRow)

CodePudding user response:

My Roasters table has field names of Name, City, and Zip. They are bond to labels on the BindingNavigator form and also a DataGridView. The BindingNavigator provides the buttons you describe.

Imports System.Data.SqlClient

Public Class BindingNavigator

Dim WithEvents bindSrc As New BindingSource

Private Sub BindingNavigator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt = LoadData1()
    bindSrc.DataSource = dt
    BindingNavigator1.BindingSource = bindSrc
    lblItem.DataBindings.Add(New Binding("Text", bindSrc, "Name"))
    lblDescription.DataBindings.Add(New Binding("Text", bindSrc, "City"))
    lblPrice.DataBindings.Add(New Binding("Text", bindSrc, "Zip"))
    DataGridView1.DataSource = bindSrc
End Sub

Private Function LoadData1() As DataTable
    Dim dt As New DataTable
    Using conn As New SqlConnection(My.Settings.CoffeeConnection),
            cmd As New SqlCommand("Select Top 10 * From Roasters;", conn)
        conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

End Class

  • Related