Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing "Next" navigation button from going to new record

Status
Not open for further replies.

Jill50

Programmer
Apr 11, 2001
21
US
I have navigation buttons on a data entry form which I disable and enable at different times; i.e. if a record is selected to edit, the buttons are disabled so that other records are unavailable until the current edit is saved. If "Last" is selected, the "Next" is disabled. However, if the "Next" is used to reach the end of the file, it will continue to create new records with defaulted fields populated. How can I prevent it from proceeding past the EOF?
Thanks for any help,
Jill50
 
This is a chunk of code from Access 97 Developers Handbook by Litwin, Getz and Gilbert from SYBEX (THE Bible), that tests the location in the recordset and enables/disables certain buttons. You could probably modify this to work for you, or I can send you the complete module if you send me an email...
Code:
Function adhEnableButtons(frm As Form)
    '
    ' Attached to the specified form's Current event.
    '
    ' This function enables and disables buttons as
    ' necessary, depending on which is the current
    ' record on the form.
    '
    ' This function counts on buttons named cmdFirst,
    ' cmdPrev, cmdNext, cmdLast and cmdNew.  One
    ' could code around this, but it seemed like
    ' overkill for this example.
    '
    ' In:
    '     frm:  A reference to the form in question.
    ' Out:
    '     Nothing

    Dim rst As Recordset
    Dim fAtNew As Integer
    Dim fUpdatable As Integer
        
    frm!txtCurrRec = frm.CurrentRecord
    Set rst = frm.RecordsetClone
    ' Sooner or later, Access will figure out
    ' how many rows there really are!
    frm!txtTotalRecs = rst.RecordCount + IIf(frm.NewRecord, 1, 0)

    ' Check to see whether or not you're on the new record.
    fAtNew = frm.NewRecord

    ' If the form isn't updatable, then you sure
    ' can't go to the new record!  If it is, then
    ' the button should be enabled unless you're already
    ' on the new record.
    fUpdatable = rst.Updatable And frm.AllowAdditions
    frm!cmdNew.Enabled = IIf(fUpdatable, Not fAtNew, False)

    If fAtNew Then
        frm!cmdNext.Enabled = False
        frm!cmdLast.Enabled = True
        frm!cmdFirst.Enabled = True And (rst.RecordCount > 0)
        frm!cmdPrev.Enabled = True And (rst.RecordCount > 0)
    Else
        ' Sync the recordset's bookmark with
        ' the form's bookmark.
        rst.Bookmark = frm.Bookmark

        ' Move backwards to check for BOF.
        rst.MovePrevious
        frm!cmdFirst.Enabled = Not rst.BOF
        frm!cmdPrev.Enabled = Not rst.BOF

        ' Get back to where you started.
        rst.Bookmark = frm.Bookmark

        ' Move forward to check for EOF.
        rst.MoveNext
        frm!cmdNext.Enabled = Not (rst.EOF Or fAtNew)
        frm!cmdLast.Enabled = Not (rst.EOF Or fAtNew)
    End If
End Function
I do suggest buying the book as it has helped me out lots of times. Terry M. Hoey
th3856@txmail.sbc.com
While I don't mind e-mail messages, please post all questions in these forums for the benefit of all members.
 
On your OnClick event for the next button, enter this code:

Private Sub btn_click()
With rst
.movenext
If .eof then .movefirst
'Enter your instructions here
end with
end sub


Good luck - Shane
 
Thanks! I have THE book, so I will try that. Shane, I tried your method already and the problem was that it was too late - it had already assigned an autonumber primary key to the field, but thanks for responding!
Jill50
 
Jill,

Chapter 8 has all the code for the Nav buttons. I have used it before and it works great, better than Ms. Access created... Terry M. Hoey
th3856@txmail.sbc.com
While I don't mind e-mail messages, please post all questions in these forums for the benefit of all members.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top