Next record button

J

Jason

I have a command button with the code below. The problem I have is that when
I first open the form, the me.recordset.recordcount = 1 (no matter how many
records I have.) I have tried using the movelast, movefirst to get the
recordcount to accurately reflect the number of records, but then the problem
I have is that the button doesn't work on the first click (but it does on the
second). What really throws me for a loop is that the if statement is
executed and it passes through the movenext command, but it doesn't go to the
next record unless you click twice.

Any help is appreciated.

Private Sub cmdNextRecord_Click()
On Error GoTo Err_cmdNext_Click

MsgBox Me.CurrentRecord & "<" & strCount
If Me.CurrentRecord < Me.Recordset.RecordCount Then
Me.Recordset.MoveNext
' MsgBox 2
Else
DoCmd.OpenForm "frmGrade"
End If

Exit_cmdNext_Click:
Exit Sub

Err_cmdNext_Click:
MsgBox Err.Description
Resume Exit_cmdNext_Click

End Sub
 
D

Dale Fye

Jason,

Best way I have found is to use the RecordSetClone property, like:

if me.CurrentRecord < me.recordsetclone.recordcount then
me.recordset.movenext
endif

But generally, I have a single set of code in my form when I build my own
record navigation buttons, and I try to disable the buttons when they should
not be available (already on the first or last record, or on a new record
that has not been saved). It looks something like:

Private Sub Buttons

Dim lngRecCount as long
Dim bNotNewRecord as Boolean

'if the screen control with the focus is one of my navigation buttons
'move the focus to a dummy textbox with zero width
'so you don't generate an error if you try to disable a control
Select Case Screen.ActiveControl.Name
case "cmd_First", "cmd_Previous", "cmd_Next", "cmd_Last"
me.txt_Dummy.setfocus
Case Else
'do nothing
End Select

lngRecCount = me.recordsetclone.recordcount
bNotNewRecord = me.newrecord

me.cmd_First.Enabled = bNotNewRecord AND (me.CurrentRecord > 0)
me.cmd_Previous.Enabled = bNotNewRecord AND (me.CurrentRecord > 0)
me.cmd_Next.Enabled = bNotNewRecord AND _
(me.CurrentRecord < lngRecCount - 1)
me.cmd_Last.Enabled = bNotNewRecord AND _
(me.currentrecord < lngRecCount - 1)
me.cmd_Cancel.Enabled = me.NewRecord OR me.Dirty
me.cmd_Save.Enabled = me.newrecord or me.dirty

End Sub

--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.
 
M

Marshall Barton

Jason said:
I have a command button with the code below. The problem I have is that when
I first open the form, the me.recordset.recordcount = 1 (no matter how many
records I have.) I have tried using the movelast, movefirst to get the
recordcount to accurately reflect the number of records, but then the problem
I have is that the button doesn't work on the first click (but it does on the
second). What really throws me for a loop is that the if statement is
executed and it passes through the movenext command, but it doesn't go to the
next record unless you click twice.

Private Sub cmdNextRecord_Click()
On Error GoTo Err_cmdNext_Click

MsgBox Me.CurrentRecord & "<" & strCount
If Me.CurrentRecord < Me.Recordset.RecordCount Then
Me.Recordset.MoveNext
' MsgBox 2
Else
DoCmd.OpenForm "frmGrade"
End If


Where did you put the MoveLast code?

The usual approach is to use the form's Load event:
Me.RecordsetClone.MoveLast

Because that uses RecordsetClone, there is no need to use
MoveFirst after that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top