Next Record button

rupes_mahal

Registered User.
Local time
Today, 00:00
Joined
Aug 12, 2001
Messages
60
hi..

I have created a command button using the wizard to go to the next record. It works. But the problem is, if i have 2 records already there, and click on next record, when im on record 2. It goes to a new blank record. I dont want it to do that. I want it to stop at the last record which has data in it, not to go to a new record. I have the VB code for it:


Private Sub NextRecord_Click()
On Error GoTo Err_NextRecord_Click


DoCmd.GoToRecord , , acNext

Exit_NextRecord_Click:
Exit Sub

Err_NextRecord_Click:
MsgBox Err.Description
Resume Exit_NextRecord_Click

End Sub

I dont know what code to insert for it to stop at the last record which has data in it.

Please help...

Thank you in advance
ruby
 
This is probably not the slickest method, but it should work. Immediately after your existing DoCmd statement, insert the following statement:

If Me.NewRecord Then DoCmd.GoToRecord , , acPrevious
 
[This message has been edited by AlanS (edited 08-14-2001).]
 
Thank you AlanS....it worked..

Just another quick question please, I just want to know what that line does.(wat it means and how does it work...the code line).

"If Me.NewRecord Then DoCmd.GoToRecord , , acPrevious"

Thank you in advance

ruby
 
hi AlanS..

When it gets to the last record, it now stops at the record with data in it, due to your help. When the users gets to the last record by clicking on the "Next Record" button, I want a message to appear to say "This is the last record" .

The VB code so far is as above, but here it is again:

Private Sub NextRecord_Click()
On Error GoTo Err_NextRecord_Click


DoCmd.GoToRecord , , acNext
If Me.NewRecord Then DoCmd.GoToRecord , , acPrevious
MsgBox Err.Description
Exit_NextRecord_Click
Exit Sub

Err_NextRecord_Click:
MsgBox Err.Description
Resume Exit_NextRecord_Click

End Sub

What code do I put in to do this?

Thank you in advance

Ruby
 
Hi Ruby

Between the If Me.NewRecord Then DoCmd.GoToRecord , , acPrevious line and MsgBox Err.Description line add this line of code:
MsgBox "This the last record"

That should do it for you.
Rob
 
hi Robert...

sorry I actual pasted the wrong VB code above. The correct code is:

Private Sub NextRecord_Click()
On Error GoTo Err_NextRecord_Click


DoCmd.GoToRecord , , acNext
If Me.NewRecord Then DoCmd.GoToRecord , , acPrevious

Exit_NextRecord_Click:
Exit Sub

Err_NextRecord_Click:
MsgBox Err.Description
Resume Exit_NextRecord_Click

End Sub


I tried it with the line you gave me, in between the If Me.NewRecord Then DoCmd.GoToRecord , , acPrevious line and Exit_NextRecord_Click:

but now the message keeps poping up everytime I click to go to a next record.
I only want it to pop up when I get to the last record.

Message line you gave was: MsgBox "This is the last record"...

Any ideas please.

Thank you in advance.

Ruby
 
The code below will advance to the next record; if that causes it to go the new (blank) record at the end of the table, it will then go back to the previous record, sound a beep, and display the message. "Me" is a reference to the current form object, and NewRecord is a Boolean property of the form object indicating whether the form is currently displaying a new record (True) or an existing record (False).

DoCmd.GoToRecord , , acNext
If Me.NewRecord Then
DoCmd.GoToRecord , , acPrevious
Beep
MsgBox "This is the last record."
End If
 
Yeah, but you could skip all that and just set the form's Allow Additions property to No

Darrin@CB69
 
AlanS...

I must say a big Thank you to you.....its all working.

Your help is most appreciated

Thanks
Ruby
 
You're welcome! cargobay 69 is correct about setting the AllowAdditions property to False, but that will totally prevent the addition of any new records unless and until that property is reset. If you don't ever want to add new records on this form, AllowAdditions is an easier way to go. If you do, you can either use my method or simply reset the AllowAdditions property at that time.
 
You know...
You could set the Allow Additions to False...for your form...
Create an Add New Record button that would when clicked:
Set Allow Additions to True...then
DoCmd.GotoRecord, , acNext...then
Set Allow Additions to False...

This way, the ONLY way to add a new record would be to click the button...
hmmmm
 

Users who are viewing this thread

Back
Top Bottom