Results 1 to 6 of 6

Thread: Navigating through database records

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    21

    Navigating through database records

    I'm working on making a basic program that will allow me to move through database records, and when the user is at the last record to disable the next/last record buttons, same thing with the previous/first record buttons. Here is what I've done to try and accomplish this;

    I made 2 methods and call them when the button is pressed, this doesent work atm. The problems with it are; if you hit btnToEnd it will jump to the last record but wont grey out the button until you press next again. Same thing happens with the btnToStart.

    Code:
    Private Sub MaxRecord()
            If recordCount > Me.TblStudentsBindingSource.Count - 1 Then
                btnToEnd.Enabled = False
                btnNext.Enabled = False
            Else
                btnToEnd.Enabled = True
                btnNext.Enabled = True
                recordCount = recordCount + 1
            End If
        End Sub
        Private Sub MinRecord()
            If Me.TblStudentsBindingSource.Position() = 0 Then
                btnToStart.Enabled = False
                btnPrevious.Enabled = False
            Else
                btnToStart.Enabled = True
                btnPrevious.Enabled = True
                recordCount = recordCount - 1
            End If
        End Sub
    
        Private Sub TblStudentsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.TblStudentsBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.StudDataDataSet)
    
        End Sub
    
        Private Sub btnToStart_Click(sender As Object, e As EventArgs) Handles btnToStart.Click
            MaxRecord()
            Me.TblStudentsBindingSource.MoveFirst() 'Moves to the First Record
            MaxRecord()
    
            'Me.TblStudentsBindingSource.Position() to check position of record
            'Me.TblStudentsBindingSource.Count()-1 for button disable
            '.filter to find records
        End Sub
    
        Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
            MinRecord()
            Me.TblStudentsBindingSource.MovePrevious() 'Moves back 1 record
            MinRecord()
    
        End Sub
    
        Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
            MaxRecord()
            Me.TblStudentsBindingSource.MoveNext()
            MaxRecord()
    
        End Sub
    
        Private Sub btnToEnd_Click(sender As Object, e As EventArgs) Handles btnToEnd.Click
            MaxRecord()
            Me.TblStudentsBindingSource.MoveLast()
            MaxRecord()
    
        End Sub

  2. #2
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Navigating through database records

    Is there a reason why you are re-inventing the wheel, so to speak? There is already a BindingNavigator control that you can use to navigate through the records in a database...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    21

    Re: Navigating through database records

    The navigator is part of the bar with like save and other stuff on the form, I realize this and I was told to disable the bar because "users are stupid." Professor asked that we create our own function to do this.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Navigating through database records

    I'm not really sure what the purpose of MaxRecord or MinRecord are nor do I understand what the variable recordCount is supposed to store as you are not changing the number of records when you increment / decrement that variable.... What I would do is something like:
    Code:
        Private Sub btnToStart_Click(sender As Object, e As EventArgs) Handles btnToStart.Click
            Me.TblStudentsBindingSource.MoveFirst() 'Moves to the First Record
            btnToStart.Enabled = False
            btnPrevious.Enabled = False
            btnToEnd.Enabled = True
            btnNext.Enabled = True
        End Sub
    
        Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
            Me.TblStudentsBindingSource.MovePrevious() 'Moves back 1 record
            If Me.TblStudentsBindingSource.Position() = 0 Then
                btnToStart.Enabled = False
                btnPrevious.Enabled = False
            End If
            btnToEnd.Enabled = True
            btnNext.Enabled = True
        End Sub
    
        Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
            Me.TblStudentsBindingSource.MoveNext()
            If Me.TblStudentsBindingSource.Position() = Me.TblStudentsBindingSource.Count - 1 Then
                btnToEnd.Enabled = False
                btnNext.Enabled = False
            End If
            btnToStart.Enabled = True
            btnPrevious.Enabled = True
        End Sub
    
        Private Sub btnToEnd_Click(sender As Object, e As EventArgs) Handles btnToEnd.Click
            Me.TblStudentsBindingSource.MoveLast()
            btnToEnd.Enabled = False
            btnNext.Enabled = False
            btnToStart.Enabled = True
            btnPrevious.Enabled = True
        End Sub
    Last edited by Pyth007; Feb 19th, 2015 at 02:17 PM. Reason: Fixed Typo (as next post pointed out)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2015
    Posts
    21

    Re: Navigating through database records

    I was kinda lost in what I was supposed to do, this clears that up. But I still run into the problem of if I go forward all the way to the end, then go back all of the buttons are blocked and I'm not sure how to fix that.

    EDIT: found the error, those two buttons are meant to be = true otherwise the buttons will grey out

    Code:
            btnToStart.Enabled = False
                btnPrevious.Enabled = False
    Last edited by NewtoV; Feb 18th, 2015 at 06:50 PM.

  6. #6
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: Navigating through database records

    Oops, you're right! I fixed my code above. Alternatively you could create a Sub that enables the buttons correctly...
    Code:
        Public Sub FixButtons()
            ' First make all of the buttons Enabled...
            btnToStart.Enabled = True
            btnPrevious.Enabled = True
            btnToEnd.Enabled = True
            btnNext.Enabled = True
    
            ' Now disable those buttons for the fringe cases...
            Dim index As Integer = Me.TblStudentsBindingSource.Position()
            If index = Me.TblStudentsBindingSource.Count - 1 Then
                btnToEnd.Enabled = False
                btnNext.Enabled = False
            ElseIf index  = 0 Then
                btnToStart.Enabled = False
                btnPrevious.Enabled = False
            End If
        End Sub
    And then just call that Sub after each "movement". For example:
    Code:
        Private Sub btnToStart_Click(sender As Object, e As EventArgs) Handles btnToStart.Click
            Me.TblStudentsBindingSource.MoveFirst() 'Moves to the First Record
            FixButtons()
        End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width