Results 1 to 7 of 7
  1. #1
    gafort is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Location
    Cleveland, Ohio
    Posts
    22

    End of table code


    I have a form with next and previous buttons to advance through records. They work until I get to record 1 or record 300. What code should I use, and where, to continue scrolling through the table to avoid runtime error?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    One method is to handle the error, another is to use RecordsetClone to check if at BOF or EOF. For starters review http://www.vbaexpress.com/forum/showthread.php?t=19086
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Here's what I use for this. If you don't want the Messagebox to pop, simply remark it out, and the code will simply not allow Access to attempt to move out of the range of Records.

    Code:
    Private Sub Next_Click()
      If CurrentRecord = RecordsetClone.RecordCount Then
        MsgBox "You are on the Last Record!"
      Else
        DoCmd.GoToRecord , , acNext
      End If
    End Sub
    
    Private Sub Previous_Click()
      If CurrentRecord = 1 Then
        MsgBox "You are on the First Record!"
      Else
       DoCmd.GoToRecord , , acPrevious
      End If
    End Sub


    Linq ;0)>

  4. #4
    gafort is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Location
    Cleveland, Ohio
    Posts
    22
    Thanks Linq, this one is a little trickier. Here is the code after I inserted your suggestions. Can you see what I did wrong?

    Option Compare Database


    Private Sub Add_New_Click()
    DoCmd.GoToRecord , , acNewRec
    End Sub


    Private Sub btnVisitFacebook_Click()


    If Nz(Me.Facebook, "") = "" Then
    MsgBox "The Facebook Address is Empty!"
    Cancel = True
    Facebook.SetFocus
    Exit Sub
    End If
    Application.FollowHyperlink [Facebook]
    End Sub


    Private Sub btnVisitWebsite_Click()
    If Nz(Me.Website, "") = "" Then
    MsgBox "The Website Address is Empty!"
    Cancel = True
    Website.SetFocus
    Exit Sub
    End If
    Application.FollowHyperlink [Website]
    End Sub


    Private Sub Next_Click()
    If CurrentRecord = RecordsetClone.RecordCount Then
    DoCmd.GoToRecord , , acNext
    End If
    End Sub


    Private Sub Next_GotFocus()
    Me.Refresh
    End Sub


    Private Sub Previous_Click()
    If CurrentRecord = 1 Then
    DoCmd.GoToRecord , , acPrevious
    End If
    End Sub


    Private Sub Previous_GotFocus()
    Me.Refresh
    End Sub


    Private Sub Questions_Click()
    DoCmd.OpenForm "Questions", acNormal
    DoCmd.Close acForm, Me.Name
    End Sub




    Private Sub Save_Record_Click()
    DoCmd.RunCommand acCmdSaveRecord
    End Sub

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    In trying to eliminate the user warnings (I guess that's what you were trying to do) you mixed up what to dump and what to keep. This will do it, assuming that the buttons are named Next and Previous.

    Code:
    Private Sub Next_Click()
      If CurrentRecord = RecordsetClone.RecordCount Then
        'Do nothing
      Else
        DoCmd.GoToRecord , , acNext
      End If
    End Sub
    
    Private Sub Previous_Click()
      If CurrentRecord = 1 Then
        'Do nothing
      Else
       DoCmd.GoToRecord , , acPrevious
      End If
    End Sub


    Not sure exactly what you're trying to do with this

    Code:
    Private Sub Next_GotFocus()
     Me.Refresh
    End Sub

    and the similar code in Previous_GotFocus event, but I suspect this is superfluous.

    Linq ;0)>

  6. #6
    gafort is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2011
    Location
    Cleveland, Ohio
    Posts
    22
    Once again, thank you Linq, I owe you big time. that code worked perfectly. the following code was inserted because I thought i might lose user input data if they did not save. is it not needed? I wanted to automatically save verses a manual save click.

    Not sure exactly what you're trying to do with this

    Code:
    Private Sub Next_GotFocus()
     Me.Refresh
    End Sub

    and the similar code in Previous_GotFocus event, but I suspect this is superfluous.

    Linq ;0)>
    Last edited by June7; 04-03-2013 at 10:42 PM. Reason: fix quote tag

  7. #7
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    In Access, when you

    1. Move to another Record
    2. Close a Form
    3. Close Access itself

    The Record is automatically saved!

    MS Access
    is classified as a Rapid Application Development tool because when used the way it is intended to be used, i.e. with Bound Forms, the Access Gnomes take care of the vast majority of the work involved, like saving a Record, unlike most apps, where the developer has to code even the most mundane tasks!

    Me.Requery and Me.Refresh will also save the Current Record, but by Default, Access performs a Refresh every 60 seconds without user/developer interaction.

    There is one caveat which applies to the numbered list, above:

    If you use a custom Command Button to close a Form, using

    DoCmd.Close

    you need to first explicitly Save the Record, either using

    DoCmd.RunCommand acCmdSaveRecord

    or

    If Me.Dirty Then Me.Dirty = False

    so you should always have
    Code:
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.Close
    or

    Code:
    If Me.Dirty Then Me.Dirty = False
    DoCmd.Close
    Because of a quirk in Access, when DoCmd.Close is used, Access closes the Form regardless of whether or not a PK field or other Required Field has been left blank or Validation rules have been violated!

    If one of these things occur, Access will simply dump the Record, Close the Form, and not tell the user that the Record has been dumped!

    The code If Me.Dirty Then Me.Dirty = False or DoCmd.RunCommand acCmdSaveRecord
    forces Access to attempt to Save the Record, and if a violation has occurred, will throw up a warning message, allowing correction to be made before Closing the Form.

    Linq ;0)>

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Update to a table with code
    By jlk in forum Programming
    Replies: 4
    Last Post: 01-07-2013, 03:37 AM
  2. Storing code in a table
    By jgelpi16 in forum Programming
    Replies: 4
    Last Post: 07-27-2011, 07:22 PM
  3. Replies: 7
    Last Post: 04-15-2011, 08:46 AM
  4. Add fields to a table through code
    By Riamor in forum Programming
    Replies: 2
    Last Post: 07-21-2010, 07:06 AM
  5. Replacement in table with VB code
    By miziri in forum Access
    Replies: 2
    Last Post: 06-28-2010, 01:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums