Home and Learn: VB Net Course


Coding for the Navigate Buttons

Part of an ongoing tutorial. This lessons is part of an ongoing tutorial. The first part is here:

Coding your own VB .NET database projects

In the last lesson, you set up a Form with four buttons and two textboxes. In this lesson, you'll add the code for the buttons.

 

How to Move Forward One Record at a Time

Double click your Next Record button to access the code. Add the following If … Else Statement:

If inc <> MaxRows - 1 Then

inc = inc + 1

NavigateRecords()

Else

MessageBox.Show("No More Rows")

End If

We're checking to see if the value in inc does not equal the value in MaxRows - 1. If they are both equal then we know we've reached the last record in the DataSet. In which case, we just display a message box. If they are not equal, these two lines get executed:

inc = inc + 1
NavigateRecords()

First, we move the inc counter on by one. Then we call the Sub we set up:

NavigateRecords()

Our Subroutine is where the action takes place, and the values from the DataSet are placed in the textboxes. Here it is again:

Private Sub NavigateRecords()

txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)
txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2)

End Sub

The part that moves the record forward (and backwards soon) is this part:

Rows( inc )

Previously, we hard-coded this with:

Rows( 0 )

Now the value is coming from the variable called inc. Because we're incrementing this variable with code, the value will change each time the button is clicked. And so a different record will be displayed.

You can test out your Next button. Run your programme and click the button. You should now be able to move forward through the DataSet. When you get to the end, you should see the message box display "No More Rows".

None of the other button will work yet, of course. So let's move backwards.

 

Move Back One Record at a Time

To move backwards through the DataSet, we need to decrement the inc counter. All this means is deducting 1 from whatever is currently in inc.

But we also need to check that inc doesn't go past zero, which is the first record in the DataSet. Here's the code to add to your btnPrevious:

If inc > 0 Then

inc = inc - 1

NavigateRecords()

Else

MessageBox.Show("First Record")

End If

So the If statement first checks that inc is greater than zero. If it is, inc gets 1 deducted from. Then the NavigateRecords() subroutine gets called. If inc is zero or less, then we display a message.

When you've finished adding the code, test your programme out. Click the Previous button first. The message box should display, even though no records have been loaded into the textboxes. This is because the variable inc has a value of -1 when the form first loads. It only gets moved on to zero when the Next button is clicked. You could amend your IF Statement to this:

If inc > 0 Then

inc = inc - 1

NavigateRecords()

ElseIf inc = -1 Then

MessageBox.Show("No Records Yet")

ElseIf inc = 0 Then

MessageBox.Show("First Record")

End If

This new If Statement now checks to see if inc is equal to minus 1, and displays a message if it does. It also checks if inc is equal to zero, and displays the "First Record" message box.

 

Moving to the Last Record in the DataSet

To jump to the last record in the DataSet, you only need to know how many records have been loaded into the DataSet - the MaxRows variable in our code. You can then set the inc counter to that value, but minus 1. Here's the code to add to your btnLast:

If inc <> MaxRows - 1 Then

inc = MaxRows - 1

NavigateRecords()

End If

The reason we're saying MaxRows - 1 is that the row count might be 5, say, but the first record in the DataSet starts at zero. So the total number of records would be zero to 4. Inside of the If Statement, we're setting the inc counter to MaxRows - 1, then calling the NavigateRecords() subroutine.

That's all we need to do. So run your programme. Click the Last button, and you should see the last record displayed in your textboxes.

 

Moving to the First Record in the DataSet

Moving to the first record is fairly straightforward. We only need to set the inc counter to zero, if it's not already at that value. Then call the Sub:

If inc <> 0 Then

inc = 0

NavigateRecords()

End If

Add the code to your btnFirst. Run your programme and test out all of your buttons. You should be able to move through the names in the database, and jump to the first and last records.

As yet, though, we don't have a way to add new records, to update records, or to delete them. Let's do that next.

Back to the VB NET Contents Page

 


Buy the Book of this Course

Email us: enquiry at homeandlearn.co.uk