Consulting

Results 1 to 5 of 5

Thread: Previous record command button

  1. #1

    Previous record command button

    [VBA]Private Sub cmdPriorRecord_Click()
    On Error GoTo Err_cmdPriorRecord_Click
    DoCmd.GoToRecord , , acPrevious
    Exit_cmdPriorRecord_Click:
    Exit Sub
    Err_cmdPriorRecord_Click:
    MsgBox Err.Description
    Resume Exit_cmdPriorRecord_Click

    End Sub[/VBA]

    Why does this error when it is at the first record in my form?

  2. #2
    VBAX Mentor
    Joined
    Dec 2007
    Posts
    462
    Location
    Access is weird somtimes, so here's one of mine with out the command button, with warning msg:
    [vba]
    DoCmd.GoToRecord , , acPrevious
    Exit_Previous_Record_Button_Click:
    Exit Sub
    Err_Previous_Record_Button_Click:
    MsgBox "You cannot go backward any further", _
    vbOKOnly + vbInformation, "Can't go backward ..."
    Resume Exit_(your command button here)_Click
    Endd:
    End Sub
    [/vba]

  3. #3
    It errors on the first item because there is no previous item. You should check that you're not at BOF before calling the DoCmd and then shift to EOF if you are and want it to loop round or ignore the request (or msgbox) otherwise.

  4. #4
    Quote Originally Posted by ben.oates
    It errors on the first item because there is no previous item. You should check that you're not at BOF before calling the DoCmd and then shift to EOF if you are and want it to loop round or ignore the request (or msgbox) otherwise.
    I like that idea and I tried to build it into an IF statement but I keep getting the message: Variable not Defined? Is the BOF not applicable to a form?

  5. #5
    VBAX Master CreganTur's Avatar
    Joined
    Jan 2008
    Location
    Greensboro, NC
    Posts
    1,675
    Location
    No, you can use EOF and BOF in forms- the issue is that you're telling Acces to look for a BOF or EOF, but not telling it what recordset to use- it's undefined.

    Try something like:[vba]Dim rs As Recordset
    Set rs = Me.Recordset.Clone'<<<make clone of form's recordset

    'Check to see if at beginning of recordset
    If rs.BOF Then
    'Some Action
    ElseIf rs.EOF Then
    'Some Action
    End If[/vba]
    -Randy Shea
    I'm a programmer, but I'm also pro-grammar!
    If your issue is resolved, please use Thread Tools to mark your thread as Solved!

    PODA (Professional Office Developers Association) | Certifiable | MOS: Access 2003


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •