Next Record Button not working (1 Viewer)

Arman

Registered User.
Local time
Tomorrow, 00:05
Joined
Dec 21, 2017
Messages
19
Hello folks!

So, I've been doing some Access stuff in VBA. I managed to add "previous record" and "next record" buttons to a form that would display different info depending on the the record.

The problem is, my "Next Record" button doesn't work at all. When I load the form and click "Next", nothing happens. It doesn't refresh the form or do anything.

However, when I click on the "Previous Record" button, it works perfectly fine. Then, if I try to click on the Next button again, it works.

Basically, when the form loads, unless I first go to the previous record (the form loads the 1st record by default), I can't go to the next one. This is my code:

Code:
Private Sub btn_Next_Click()
    'MsgBox Me.CurrentRecord
    'MsgBox Me.Recordset.RecordCount
    If Me.CurrentRecord < Me.Recordset.RecordCount Then
        DoCmd.GoToRecord , , acNext
    Else
        DoCmd.GoToRecord , , acFirst
    End If
    
    Me.imgBanner.Picture = GetImagePath() & "Banners/" & Me.txt_Banner.Value
    Me.lbl_GameName.Caption = Me.GameName.Value
    Me.lbl_ReleaseDate.Caption = "Release date: " & Me.ReleaseDate.Value
    Me.lbl_MaxSlot.Caption = "Max Slots: " & Me.MaxSlot.Value

    If IsNull(Me.OfficialWebsite.Value) Or Me.OfficialWebsite.Value = "" Then
        Me.lbl_Website.Caption = "Website: " & "N/A"
    Else
        Me.lbl_Website.Caption = "Website: " & vbNewLine & Me.OfficialWebsite.Value
    End If
End Sub

Private Sub btn_Previous_Click()
    If Me.CurrentRecord = 1 Then
        DoCmd.GoToRecord , , acLast
    Else
        DoCmd.GoToRecord , , acPrevious
        
    End If
    
    Me.imgBanner.Picture = GetImagePath() & "Banners/" & Me.txt_Banner.Value
    Me.lbl_GameName.Caption = Me.GameName.Value
    Me.lbl_ReleaseDate.Caption = "Release date: " & Me.ReleaseDate.Value
    Me.lbl_MaxSlot.Caption = "Max Slots: " & Me.MaxSlot.Value
    
    If IsNull(Me.OfficialWebsite.Value) Or Me.OfficialWebsite.Value = "" Then
        Me.lbl_Website.Caption = "Website: " & "N/A"
    Else
        Me.lbl_Website.Caption = "Website: " & vbNewLine & Me.OfficialWebsite.Value
    End If
End Sub

Any idea what the issue could be? It worked perfectly fine until today. I may have messed up, but I genuinely have no idea what the issue is.
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 23:05
Joined
Jan 14, 2017
Messages
18,258
I'm guessing the btnNext code is defaulting to the else part and always sending you to where you already are - at the first record.

If so, it suggests Me.Recordset.RecordCount isn't giving the result you are expecting.

Try enabling the two message box lines or use breakpoints to check what happens in the if ...else... End if section when you run that code
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:05
Joined
May 7, 2009
Messages
19,246
you can test for the Recordset's EOF:
Code:
Private Sub btn_Next_Click()
	DoCmd.GoToRecord, , acNext
	If Me.Recordset.EOF Then 
		DoCmd.GoToRecord, , acFirst
	End If
	...
	...
	...
End Sub

Private Sub btn_Previous_Click()
	DoCmd.GoToRecord, , acPrevious
	If Me.Recordset.BOF Then
		DoCmd.GoToRecord, , acLast
	End If
	...
	...
	...
End Sub
 

Arman

Registered User.
Local time
Tomorrow, 00:05
Joined
Dec 21, 2017
Messages
19
I'm guessing the btnNext code is defaulting to the else part and always sending you to where you already are - at the first record.

If so, it suggests Me.Recordset.RecordCount isn't giving the result you are expecting.

Try enabling the two message box lines or use breakpoints to check what happens in the if ...else... End if section when you run that code
That was what I originally tried, but no matter what workaround I hoped for, none actually managed to modify the RecordCount for some reason.




you can test for the Recordset's EOF:
Code:
Private Sub btn_Next_Click()
	DoCmd.GoToRecord, , acNext
	If Me.Recordset.EOF Then 
		DoCmd.GoToRecord, , acFirst
	End If
	...
	...
	...
End Sub

Private Sub btn_Previous_Click()
	DoCmd.GoToRecord, , acPrevious
	If Me.Recordset.BOF Then
		DoCmd.GoToRecord, , acLast
	End If
	...
	...
	...
End Sub

Well, this approach works perfectly when I use it except when I reach the end of the records, I do get a Run-Time Error.
 

isladogs

MVP / VIP
Local time
Today, 23:05
Joined
Jan 14, 2017
Messages
18,258
What record count do you get? 1?

I just tried putting Debug.Print Me.Recordset.RecordCount in the Form_Load & Form_Activate events of a sample form.
I also tried Me.RecordsetClone.RecordCount
In each case, the result was 1

In the Form_Current event, it gave the correct answer
So I suggest you do one of the following:

1. In Form_Current add the lines
Code:
Dim N As Integer
N = Me.Recordset.RecordCount

Then in your cmdNext event, use
Code:
If Me.CurrentRecord < N Then

2. In cmdNext event use:

Code:
N = DCount("*", "MyRecordSource") [COLOR="Red"][I]'enter your form record source here[/I][/COLOR]
If Me.CurrentRecord < N Then

Alternatively, have a look at the attached code I use for my 4 nav buttons: First/Last/Next/Prev
This covers all possible situations & also enables / disables one or more buttons depending on the record in use
 

Attachments

  • Nav Button code.txt
    2.1 KB · Views: 102

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:05
Joined
May 7, 2009
Messages
19,246
Code:
Private Sub btn_Next_Click()
        If not me.recordset.eof then

	DoCmd.GoToRecord, , acNext
	If Me.Recordset.EOF Then 
		DoCmd.GoToRecord, , acFirst
	End If
Else
DoCmd.GoToRecord, , acFirst
End if
	...
	...
	...
End Sub

Private Sub btn_Previous_Click()
If not me.recordset.bof then
	DoCmd.GoToRecord, , acPrevious
	If Me.Recordset.BOF Then
		DoCmd.GoToRecord, , acLast
	End If
Else
DoCmd.GoToRecord, , acLast
End if
	...
	...
	...
End Sub
 

Arman

Registered User.
Local time
Tomorrow, 00:05
Joined
Dec 21, 2017
Messages
19
Quick update:

Earlier today, prior to the recent two replies, I actually seem to have found a solution that works properly (until it randomly crashes someday just like the previous one):

Code:
DoCmd.GoToRecord , , acNext
If Me.CurrentRecord > Me.Recordset.RecordCount Then
        DoCmd.GoToRecord , , acFirst
End If
 

Users who are viewing this thread

Top Bottom